If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > 3 table outer join problem

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #16 (permalink)  
Old 08-12-09, 13:34
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Code:
SELECT A.ID
     , SUM(C.TranAmount) AS sum_trans
  FROM A
LEFT OUTER
  JOIN B
    ON B.ID = A.ID
LEFT OUTER
  JOIN C
    ON C.ID = A.ID + B.CSEQS
LEFT OUTER
  JOIN ( SELECT ID
              , SUM(Amount) AS D_amount
           FROM D
         GROUP
             BY ID ) AS DSUM
    ON DSUM.ID = A.ID
GROUP
    BY A.ID
HAVING COALESCE(DSUM.D_amount,0) - COALESCE(SUM(C.TranAmount),0)
       BETWEEN 100 AND 200
the sum for C could've been handled this way as well, with the GROUP BY in the joined subquery rather than in the outer query
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #17 (permalink)  
Old 08-12-09, 14:24
bfp bfp is offline
Registered User
 
Join Date: Jul 2009
Posts: 12
3 table outer join problem

Thanks again...that appears to be returning the correct results. I did try making the other SUM change as you specified as I think in the end it will be easier for me to generate this query from my program. However, my attempt ended up yielding more rows than I expected (I wrapped a SELECT DISTINCT...around it but it didn't make any difference). I probably misunderstood what you meant. Here's my attempt:

SELECT A.ID
FROM A
LEFT OUTER
JOIN B
ON B.ID = A.ID
LEFT OUTER
JOIN ( SELECT ID
, SUM(TranAmount) AS C_amount
FROM C
GROUP
BY ID ) AS CSUM
ON CSUM.ID = A.ID + B.CSEQS
LEFT OUTER
JOIN ( SELECT ID
, SUM(Amount) AS D_amount
FROM D
GROUP
BY ID ) AS DSUM
ON DSUM.ID = A.ID
GROUP
BY A.ID, DSUM.D_amount, CSUM.C_amount
HAVING COALESCE(DSUM.D_amount,0) - COALESCE(CSUM.C_amount,0)
BETWEEN 100 AND 200
Reply With Quote
  #18 (permalink)  
Old 08-12-09, 23:18
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
yeah, you're right, it's too hard that way, just leave it the way it was when it worked
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #19 (permalink)  
Old 08-12-09, 23:29
bfp bfp is offline
Registered User
 
Join Date: Jul 2009
Posts: 12
3 table outer join problem

Thanks for the clarification
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On