Gurus,
I am wondering if there can be a more efficient way of converting the "one to many relation output" to "One to one"
Without using subquery.
TableStructures
create table orders (OrderID int, SalesManID int, amount int) insert into orders select 1, 11,1000 insert into orders select 2, 22,2000 insert into orders select 3, 33,3000 insert into orders select 4, 44,4000 insert into orders select 5, 11,4000 GO create table orderDetails (OrderDetailID int, OrderId int, ItemName varchar(500), ItemQuantity int) insert into orderDetails select 100, 1, 'Juice' ,100 insert into orderDetails select 101, 1, 'Carrots' ,200 insert into orderDetails select 102, 2, 'Biscuts' ,150 insert into orderDetails select 103, 3, 'salt' ,150 insert into orderDetails select 104, 4, 'Pepper' ,160 insert into orderDetails select 105, 5, 'Perfume' ,110 insert into orderDetails select 106, 3, 'soap' ,190 insert into orderDetails select 107, 1, 'Paper' ,850
Now we can use the subqueries and have the one row instead of many that a join will typically yield.
An example of a subquery to return the expected output
SELECT O.OrderId, (select SUM(OD.itemquantity) from orderDetails OD where OD.OrderId = o.OrderID AND itemname not like 'juice' ) ItemquantitySum FROM orders O
Output will be
Question: Is there a way that the same output can be achieved using the join or any other way?
Thanks in advance for your help.
Cheers