create table tb(id int,    classid int,     value int,   datatime datetime)
insert into tb
select 1,       1   ,       233 ,   '2005-4-1'
union all select 1    ,   1    ,      222 ,   '2005-4-2'
union all select 1    ,   2    ,      234   , '2005-4-1'
union all select 1    ,   2   ,       244  ,  '2005-4-2'declare @s1 nvarchar(4000)
select @s1=''select @s1=@s1+','+quotename('(id,classid)('+rtrim(id)+','+rtrim(classid)+')')+'=sum(case when id='+rtrim(id)+' and classid='+rtrim(classid)+' then value end)' from tb group by id,classid
select @s1='select datatime' + @s1 + ' from tb group by datatime'
exec(@s1)drop table tb