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
 query

Author  Topic 

gagani
Posting Yak Master

112 Posts

Posted - 2012-05-09 : 05:24:58
for the given table,

orderid product quantity
100 A 1
100 B 2
101 B 4
101 A 3
101 C 1
101 D 3
100 A 2
101 D 5

I need a query for the output

orderid product quantity
100 A 3
100 B 2
101 B 4
101 A 3
101 C 1
101 D 8

for a given orderid, if there are two entries for the
same product, the quantities are summed up.

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2012-05-09 : 05:44:49
SELECT OrderID, Product, SUM(Quantity) AS Quantity
FROM dbo.GivenTable
GROUP BY OrderID, Product
ORDER BY OrderID, Product



N 56°04'39.26"
E 12°55'05.63"
Go to Top of Page

vinu.vijayan
Posting Yak Master

227 Posts

Posted - 2012-05-09 : 06:25:03
quote:
Originally posted by SwePeso

SELECT OrderID, Product, SUM(Quantity) AS Quantity
FROM dbo.GivenTable
GROUP BY OrderID, Product
ORDER BY OrderID, Product



N 56°04'39.26"
E 12°55'05.63"




+1 Swepeso.
Can't be done any better.

N 28° 33' 11.93148"
E 77° 14' 33.66384"
Go to Top of Page
   

- Advertisement -