Thursday, December 26, 2013

Need SQL script to get the 6 months data month wise

PFB Code



Create table #table1
(
depid int ,
dateoftransaction datetime,
empcount int
)
insert into #table1
select 100,'3/6/2013',2 union all
select 100, '4/6/2013',4 union all
select 100, '3/7/2013',4
declare @text varchar(max)
select @text = isnull(@text+',','') + convert(varchar, mon) +'--'+ convert(varchar,empcount) from
(
select Left(DATENAME(Month,dateoftransaction),3) mon, SUM(empcount) empcount from #table1
where dateoftransaction > dateadd (MONTH, -6, GETDATE())
Group by Left(DATENAME(Month,dateoftransaction),3)
)a
select @text

Thanks


Saravana Kumar C


No comments:

Post a Comment