Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Question on joins


Question on joins

Author
Message
Duran
Duran
Mr or Mrs. 500
Mr or Mrs. 500 (545 reputation)Mr or Mrs. 500 (545 reputation)Mr or Mrs. 500 (545 reputation)Mr or Mrs. 500 (545 reputation)Mr or Mrs. 500 (545 reputation)Mr or Mrs. 500 (545 reputation)Mr or Mrs. 500 (545 reputation)Mr or Mrs. 500 (545 reputation)

Group: General Forum Members
Points: 545 Visits: 1702
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.
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)SSCrazy Eights (8.5K reputation)

Group: General Forum Members
Points: 8476 Visits: 18082
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.
General Disclaimer:
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?


How to post data/code on a forum to get the best help: Option 1 / Option 2
SQLRNNR
SQLRNNR
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21055 Visits: 18258
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

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