|
-----Original Message-----
From: midrange-l-bounces@xxxxxxxxxxxx
[mailto:midrange-l-bounces@xxxxxxxxxxxx] On Behalf Of Pete Helgren
Sent: Thursday, August 09, 2007 9:59 AM
To: Midrange Systems Technical Discussion
Subject: SQL Concepts - Header and Detail
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 is the Midrange Systems Technical Discussion
(MIDRANGE-L) mailing list To post a message email:
MIDRANGE-L@xxxxxxxxxxxx To subscribe, unsubscribe, or change
list options,
visit: http://lists.midrange.com/mailman/listinfo/midrange-l
or email: MIDRANGE-L-request@xxxxxxxxxxxx Before posting,
please take a moment to review the archives at
http://archive.midrange.com/midrange-l.
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2025 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.