Create table T (Class varchar(2),CallDate datetime, CallCount int)
insert into T select ‘1‘,‘2005-8-8‘,40 union all select ‘1‘,‘2005-8-7‘,6 union all select ‘2‘,‘2005-8-8‘,77 union all select ‘3‘,‘2005-8-9‘,33 union all select ‘3‘,‘2005-8-8‘,9 union all select ‘3‘,‘2005-8-7‘,21 select * from t \ declare @s varchar(8000) set @s=‘select CallDate ‘ print @s select @s=@s+‘,[CallCount‘+Class+‘]=sum(case when Class=‘‘‘+Class+‘‘‘ then CallCount else 0 end)‘ from T where class=1 or class=2 or class=3 group by Class print @s set @s=@s+‘ from T group by CallDate order by CallDate desc ‘ exec(@s) |
|