August 26, 2015 at 2:57 pm
hi ,
My data look like this:
ITEMID DESC QTY PRICE
134 ITEM1 4 $200
134 ITEM1 1 $200
134 ITEM1 1 $200
Can you please advice me how do I write SQL stmt to add the quantity if the itemid is the same?
The result should be:
ITEMID DESC QTY PRICE
134 ITEM1 6 $200
Thanks,
Dee
August 26, 2015 at 4:12 pm
You'll need to either group by all of the items you don't want summed (added together) or run a separate subquery to get the summed values as required, which would have worse performance.
Try this:
SELECT ITEMS.ITEMID
, ITEMS.DESC
, SUM(QTY) AS QTY
, ITEMS.PRICE
FROM ITEMS
GROUP BY ITEMS.ITEMID
, ITEMS.DESC
, ITEMS.PRICE
August 26, 2015 at 5:46 pm
Dee Dee-422077 (8/26/2015)
hi ,My data look like this:
ITEMID DESC QTY PRICE
134 ITEM1 4 $200
134 ITEM1 1 $200
134 ITEM1 1 $200
Can you please advice me how do I write SQL stmt to add the quantity if the itemid is the same?
The result should be:
ITEMID DESC QTY PRICE
134 ITEM1 6 $200
Thanks,
Dee
Gosh... please don't take this as being mean but this is very, very basic. If you're going to have to write SQL code and this isn't for some course, I'd strongly recommend you get some training or at least read a book or two on the subject. If you're going to be writing SQL a lot, then I recommend you spend <$60USD and buy the Developer's Edition of SQL Server, download "Books Online", and visit a site like W3Schools.com and start training yourself.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply