SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Help on multiple table left join (outer join)


Help on multiple table left join (outer join)

Author
Message
Joe Contreras-290946
Joe Contreras-290946
Say Hey Kid
Say Hey Kid (695 reputation)Say Hey Kid (695 reputation)Say Hey Kid (695 reputation)Say Hey Kid (695 reputation)Say Hey Kid (695 reputation)Say Hey Kid (695 reputation)Say Hey Kid (695 reputation)Say Hey Kid (695 reputation)

Group: General Forum Members
Points: 695 Visits: 206
Hi All,

I'm hitting a wall(don't know why). I have four tables. Table one contains user information, Table 2 contains actual orders, Table 3 contains future orders and Table 4 contains all invoices that the user has accumulated. Table 2 ; Table 3; Table 4 will have different data having UserId as a common field. I'll need to do an outer join to get the total count for a user my resultset needs to look something like this:

Username Orders Placed Orders Paid Invoices
John Doe 20 30 24
Jane Doe 5 25 10
John Smith 0 10 0

All tables have different data but I need to get the counts for all tables for each user. What is the best way to get this done?

Thank You in advanced.
goodguy
goodguy
SSCommitted
SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)SSCommitted (1.9K reputation)

Group: General Forum Members
Points: 1916 Visits: 1037
I have 4 tables. Table1 contains user information, Table2 contains actual orders, Table3 contains future orders and Table4 contains all invoices that the user has accumulated. Table 2, Table3 and Table4 will have different data having UserId as a common field. I'll need to do an outer join to get the total count for a user my resultset needs to look something like this:
Username Orders Placed Orders Paid Invoices
John Doe 20 30 24
Jane Doe 5 25 10
John Smith 0 10 0

All tables have different data but I need to get the counts for all tables for each user. What is the best way to get this done?


First of all, it is poor DB design to have separate tables for for current and future orders; better to have them together and keep a Flag column to distinguish between them.

Table4 "contains all invoices that the user has accumulated", but your resultset column is headed "Paid Invoices", so I assume you have a flag for flagging an invoice once it is paid.

Use LEFT OUTER JOIN in your query as follows:
SELECT a.CUSTID, b.ORDERSCOUNT, c.PENDINGCOUNT, d.PAIDCONT
FROM TABLE1 a
LEFT OUTER JOIN
(
SELECT CUSTID, COUNT(ORDER) as ORDERSCOUNT
FROM TABLE2
GROUP BY CUSTID
) b
ON a.CUSTID = b.CUSTID

LEFT OUTER JOIN
(
SELECT CUSTID, COUNT(ORDER) as PENDINGCOUNT
FROM TABLE2
GROUP BY CUSTID
) c
ON a.CUSTID = c.CUSTID

LEFT OUTER JOIN
(
SELECT CUSTID, COUNT(ORDER) as PAIDCOUNT
FROM TABLE2
WHERE PAIDFLAG = 1
GROUP BY CUSTID
) d
ON a.CUSTID = d.CUSTID




Regards,

goodguy

Experience is a bad teacher whose exams precede its lessons

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search