Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Question on joins Expand / Collapse
Author
Message
Posted Tuesday, February 18, 2014 9:12 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Sunday, December 7, 2014 10:21 PM
Points: 423, Visits: 1,374
Hey everyone,

I wondered if someone could explain something to me, I am trying to teach myself T-SQL from a book, and I have just done a couple of exercises, the first one is wrong and the second is correct (ran against the Adventureworks DB), although both have the same tables involved, I cant work out what makes the second one correct, or to put it another way, why the results are not the same. I'd really appreciate some explanation, I cant work it out. I think it might be the order of the joins?

----2 I got this wrong, I cant work out how though
--select sum(OrderQty) as SumOfOrderQTY, p.ProductID, soh.OrderDate
--from Production.Product as p
--inner join sales.salesorderdetail as SOD
--on p.ProductID = sod.ProductID
--inner join sales.salesorderheader as SOH on soh.salesorderid = sod.SalesOrderID
--group by p.productid, soh.OrderDate;

----2 This is the correct one solution.
--select sum(orderqty) SumOfOrderQty, p.productid, soh.orderdate
--from sales.salesorderheader as soh
--inner join sales.salesorderdetail as sod
--on soh.salesorderid = sod.salesorderdetailid
--inner join production.product as p on sod.productid = p.productid
--group by p.productid, soh.orderdat

Thanks in advance for ANY help.

Regards,
D.
Post #1542638
Posted Tuesday, February 18, 2014 9:39 AM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 5:23 PM
Points: 4,049, Visits: 9,214
The only difference I can find is in the join between SalesOrderDetail and SalesOrderHeader. The problem is that I would believe that the first option is correct and the second one is incorrect.
The order of the joins won't make any difference with inner joins in this case (it might give you a syntax error if you want to use a column from a table before it's mentioned on a JOIN clause).



Luis C.
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1542654
Posted Tuesday, February 18, 2014 9:47 AM


SSCoach

SSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoachSSCoach

Group: General Forum Members
Last Login: Today @ 2:39 PM
Points: 18,064, Visits: 16,108
I agree, the second is joining incorrectly on detailid instead of orderid.

In addition, I would through an Order By on both of those queries so the results are easier to compare

ORDER BY p.ProductID,SumofOrderQty;






Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Post #1542660
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse