Monday, March 26, 2012

grouping by months

Hello everyone

starting from this example table:

code qnt date


aaa 1 21/01/2006
abc 2 24/01/2006
aaa 3 27/01/2006
asd 1 11/03/2006
wde 2 16/03/2006
aaa 1 18/03/2006

I'd like to select records grouping by months and adding quantities (qnt column) for similar codes on each moth period.

Result expected is:

code qnt month

aaa 4 01

abc 2 01

asd 1 03

wde 2 03

aaa 1 03

Is there an efficient way to do this?

Any example?

Thanks a lot

WHat aout:

SELECT code,SUM(qnt),MONTH(date)
FROM SomeTable
GROUP BY code,MONTH(Date)

HTH, Jens Suessmeyer.

|||

Gee!

I didn't realize it was so obvious..

Thanks!

|||

Would ne nice if you could mark the topic as solved that it doesn��t appear on my (any other) watch lists anymore, thanks.

-Jens-

|||

Select Sum(Qty), DatePart(m, date) from Table group by DatePart(m, date)

That should get you started.

edit: Sorry, didn't realize someone had helped.

No comments:

Post a Comment