Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Select from a nested Query

Author  Topic 

ryan.gillies
Starting Member

27 Posts

Posted - 2012-08-01 : 12:24:52
Hi everyone, I'm a bit stuck on how to do the following, I think a nested query is what I'm after but I'm not sure.

Lets say I have the following tables:
- Orders (containing OrderID, Customer)
- Items (containing OrderID, ItemID, Cost)

How would I go about building a table that included OrderID, Customer and Cost? I think I need a nested query, but I don't really know how to go about writing it, so a short working example would be a godsend!

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-08-01 : 12:38:26
Based on your description, you may need only a simple inner join:
SELECT
o.OrderId,
o.Customer,
SUM(i.Cost) as OrderCost
FROM
Orders o
INNER JOIN Items i ON i.OrderId = o.OrderId
ORDER BY
o.OrderId;
If that doesn't do it for you, can you post some sample data along with the expected output?
Go to Top of Page
   

- Advertisement -