|
you need to have a where clause after your Coalesce. The select statement in the Coalesce is not linking at all to the update clause. so, it's updating all the records in DMVDRMST to the single value it derived from the select statement. in other words, the select inside the Coalesce is returning a single value, which is being assigned to all the records in DMVDRMST. try something like this: ActSQL = 'Update DMVDRMST v + Set v.AMDUE = + Coalesce((Select Sum(i.INVAM) - Sum(p.PAYAM) + VAINVWK i + Left Outer Join + VAPAYWK p On i.VNDNR = p.VNDNR) where + i.vndnr = v.vndnr) , + Cast(0.00 as Decimal(7,2))'; On 9/11/06, Jeff Crosby <jlcrosby@xxxxxxxxxxxxxxxx> wrote:
All, I hope I'm not overstaying my welcome with too many SQL questions, but I'm really learning a lot from it, and I can't seem to find any examples that fit this scenario. Or else I'm just not hitting on the right search terms. Or else you'll tell me I'm making it much harder than it needs to be. We want to print a trial balance report "as of" a date in time. To do this, I created QTEMP work files: the vendor master file and the 2 related transaction files (open invoices and unmatched payments). Then I programmatically determined, based on dates, which transactions were "open" at that time, and inserted them into the QTEMP transaction files. Now I want to set the QTEMP vendor master file field AMDUE (amount due) to be the net of those transactions, by vendor. Here's what I tried: ActSQL = 'Update DMVDRMST v + Set v.AMDUE = + Coalesce((Select Sum(i.INVAM) - Sum(p.PAYAM) + From DMVDRMST v + Left Outer Join + VAINVWK i On v.VNDNR = i.VNDNR + Left Outer Join + VAPAYWK p On v.VNDNR = p.VNDNR), + Cast(0.00 as Decimal(7,2)))'; Exec SQL Execute Immediate :ActSQL; Don't laugh if this is really bad SQL <g>. I dumped the program afterward. Variable ActSQL has what I expect in it, and SQLSTT is 01504 (UPDATE or DELETE does not contain a WHERE clause). But AMDUE in the master has 70,662.30 dollars in _every_ record of the master. This 70,662.30 figure is the net of _all_ transactions for _all_ vendors. Am I heading the right direction? Thanks. -- Jeff Crosby Dilgard Frozen Foods, Inc. P.O. Box 13369 Ft. Wayne, IN 46868-3369 260-422-7531 The opinions expressed are my own and not necessarily the opinion of my company. Unless I say so. -- This is the RPG programming on the AS400 / iSeries (RPG400-L) mailing list To post a message email: RPG400-L@xxxxxxxxxxxx To subscribe, unsubscribe, or change list options, visit: http://lists.midrange.com/mailman/listinfo/rpg400-l or email: RPG400-L-request@xxxxxxxxxxxx Before posting, please take a moment to review the archives at http://archive.midrange.com/rpg400-l.
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.