× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



On 03 Apr 2013 11:53, Birgitta Hauser wrote:
On 03 Apr 2013 09:52, Stone, Joel wrote:
I have an order header and detail file with one to many.
<<SNIP>>
Should I be grouping the detail PRIOR to the join, so there is only
ONE header value?

But it seems like the SQL stmt is SO much simpler if I do the join
first.

I'd use the following query:

With
TotDet as
(Select OrderNo, Sum(DetQty) DetQTY
From OrderDet
Group By OrderNo
)
Select Coalesce(H.OrderNo, D.OrderNo)
, HdrQty, DetQty
From OrderHdr h
full outer join TotDet d
on h.OrderNo = d.OrderNo
Where HdrQty <> DetQty
or HdrQty is NULL
or DetQty is NULL
;


That is thorough, but is either likely to be or at least should be excessive, if one is to believe the one-to-many claim in\by the OP.? Though I suppose given the question was even asked, the vernacular of a layperson might best be presumed over that of a database person.

While I am unsure of what the OP meant by not having the aggregate query [appear] *prior* to the JOIN, my inference was that a CTE was what was alluded and thus was desired to be eliminated.?

Of course since the OP offered none of the DDL, their original query they wanted to change, or even sample data, all of that would just be speculation.


As an Amazon Associate we earn from qualifying purchases.

This thread ...

Replies:

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

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.