Advertisement

06.05.2008 at 04:43AM PDT, ID: 23459791
[x]
Attachment Details

TSQL JOIN: Multiple Left Outer Joins on same table.  How To?

Asked by michaelrobertfrench in MS SQL Server

Tables
   Customer(CustomerID, CustomerName)
   Order(OrderID, CustomerID, OrderAmt, ShipTo)

Select
L.CustomerID,
L.CustomerName,
Count(R1.ShipTo) OrdersShippedToTexas,
Count(R2.ShipTo) OrdersShippedToColorado
From
Customer L
Left Outer Join Order R1 ON (L.CustomerID = R1.CustomerID AND R1.ShipTo = 'TX')
Left Outer Join Order R2 ON (L.CustomerID = R2.CustomerID AND R2.ShipTo = 'CO')
Group By
L.CustomerID,
L.CustomerName

Can I do a multiple LOJ on the same table?

I want output like this:

CustomerID             Customer             OrdersShippedToTexas                  OrdersShippedToColorado
1                                  Susie                                                  5                                                            8
2                                   Doug                                                  7                                                            4

I have been using views where I have used table alias R1 and R2.  Views work - Tables don't. Why?
I want a simple count report where I am using the join condition to identify the wanted related records.



Start Free Trial
[+][-]06.05.2008 at 04:54AM PDT, ID: 21718253

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.05.2008 at 05:10AM PDT, ID: 21718368

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.05.2008 at 05:11AM PDT, ID: 21718384

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: MS SQL Server
Sign Up Now!
Solution Provided By: yuching
Participating Experts: 4
Solution Grade: A
 
 
[+][-]06.05.2008 at 05:11AM PDT, ID: 21718385

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]06.05.2008 at 05:44AM PDT, ID: 21718670

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]06.05.2008 at 06:41AM PDT, ID: 21719243

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628