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
 group data by month

Author  Topic 

sign_seventh
Yak Posting Veteran

66 Posts

Posted - 2008-02-20 : 05:56:20
Hi,
how can i group data using specified date like for example per month.
here is my sample data.

fielddate | fieldnumber
january 1, 2007 | 1
january 5, 2007 | 2
February 2, 2007| 3
March 4, 2007 | 3
March 5, 2007 | 4

i want to sum all fieldnumber by month
result will be:

resultdate | resultnumber
january 2007 | 3
February 2007 | 3
March 2007 | 7

thnx for the help in advance.


visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-02-20 : 06:00:58
group by CAST(MONTH(datefield) AS varchar(2))+CAST(year(datefield) AS varchar(4))
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-02-20 : 08:31:20
If the datatype of the column is DATETIME, then

Select dateadd(month,datediff(month,0,fielddate ),0) as months,sum(fieldnumber) as fieldnumber
from your_table
Group by dateadd(month,datediff(month,0,fielddate ),0)

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

sign_seventh
Yak Posting Veteran

66 Posts

Posted - 2008-02-20 : 19:34:30
nice.. thanks to both of u
Go to Top of Page
   

- Advertisement -