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
 Count by month

Author  Topic 

satheesh
Posting Yak Master

152 Posts

Posted - 2011-03-31 : 11:24:05
Hi all,

I need to find the total sales by each month for last 3 year

for eg:
select COUNT(cst.cstid) from customer
where (cst.StartDate between '2010-01-01 00:00:00.000' AND '2010-12-31 23:59:59.997')

this will give one year total sales.

But I need to find month wise.Is there any easy way to do this.Any help will be highly appreciated.


Regards,
SG

expected result:

Month totalsales
Jan 245
feb 55
--
--
Dec 12

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-03-31 : 11:32:57
[code]
select dateadd(month, datediff(month, 0, cst.StartDate), 0), COUNT(cst.cstid) from customer
where (cst.StartDate between '2010-01-01 00:00:00.000' AND '2010-12-31 23:59:59.997')
GROUP BY dateadd(month, datediff(month, 0, cst.StartDate), 0)
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

satheesh
Posting Yak Master

152 Posts

Posted - 2011-03-31 : 12:24:29
Thanks Khtan.Its working.
Go to Top of Page
   

- Advertisement -