Seems like I have been doing more of these SQL updates lately where I have a header file (single instance) with a detail file (multiple instances). I have a situation where I have a quantity field in the detail that I want to sum and then update the header. The summary on the detail is easy:

select ssn,job,ped, sum(qty) from detail
group by ssn,job,ped
order by ssn,job,ped

The resulting data set is what I need. So now I need to relate that to the header which also has the same fields, one for each instance of ssn,job, and ped (period end date).

Update header set header.qty = (select sum(qty) from detail group by ssn,job,ped order by ssn,job,ped)
**and here is were I get uncertain **
where exists
(select 1 from detail where header.ssn = detail.ssn, header.job = detail.job, header.ped = detail.ped)

Is the "where exists" clause the right choice ? Does the syntax above make sense?

What I am uncertain about is the relationship between the occurrences in the detail file and how to properly relate them to: 1. The header records and, 2. the sum of the detail records that update the header. I am not sure why I have such an issue with the sub queries that are correlated. Can't seem to visualize it.

Pete Helgren



This thread ...

Follow-Ups:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2026 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.