tsql - Is this Correlated SubQuery Correct -
i trying wrap head around sub-queries (correlated). although book explains (ms sql server 2012) i'm still little confused. using 3 tables, orders, products , orderdetails want find sum of products shipped usa. came following query not quite understand how got there except correlation referencing 'where' in outer query. can correct work , offer better explanation book?
select p.productid, p.productname, sum(qty * od.unitprice) totalamount production.products p join sales.orderdetails od on p.productid = od.productid n'usa' in (select o.shipcountry sales.orders o join sales.orderdetails od on o.orderid = od.orderid shipcountry = n'usa') group p.productid, p.productname;
select p.productid , p.productname , sum(qty * od.unitprice) [total_amount] production.products productid join sales.orderdetails od on p.productid = od.product join salesorders o on o.orderid = od.orderid o.shipcountry in ('usa') group p.productid, p.productname
the following should work - technically dont require sub query. bog down query.
Comments
Post a Comment