15.7.09

Count of records group by month in SQL Server

Scenario:



Am trying to group the records(count) based on the month. How can we achieve it?



Solution:

drop table venkat
create table venkat(id int, date1 datetime,date2 datetime)
insert into venkat values(1,'1/2/2009','3/3/2009')
insert into venkat values(1,'2/2/2009','4/3/2009')
insert into venkat values(2,'2/2/2009','3/3/2009')
insert into venkat values(2,'2/2/2009','4/3/2009')


select * from venkat


select

"MonthName"=
case when datepart(month,date1) =1 then 'January'
when datepart(month,date1) =2 then 'February'
end ,

count(*) as CountByMonth
from venkat group by datepart(month,date1)

Thanks and Regards,

Venkatesan Prabu .J

No comments:

Post a Comment