question

sqlnewb avatar image
sqlnewb asked

Group By Question

I am trying to write a query to return how many items were sold in particular months. Right now its returning the same item multiple times per month. select distinct itemID, sum(AmtSold), yrmonth from inventory where YrMonth between 201105 and 201108 Group by itemID, yearmonth, AmtSold Order by yearmonth , productid I just want each ItemID returned once per month with the Amtsold summed for the whole month per item. Result I want: item id AmtSold YrMonth 123 230 201105 456 150 201105 123 175 201106 456 122 201106
sql-server-2008group-by
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Cyborg avatar image
Cyborg answered
Just remove AmtSold from the group by list

select distinct itemID, sum(AmtSold), yrmonth
from inventory
where YrMonth between 201105 and 201108
Group by itemID, yearmonth,
Order by yearmonth , productid
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

You probably mean "remove AmtSold".
0 Likes 0 ·
Thanks Hakan!
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.