日期:2014-05-17 浏览次数:20500 次
create table #tb([date] varchar(10),[name] varchar(10),s1 int)
insert into #tb
select '2013-1-5' as [date],'a' as [name],3 as s1
union all select '2013-1-15','b',2
union all select '2013-1-16','a',2
union all select '2013-2-5','a',1
union all select '2013-2-5','b',1
union all select '2013-3-5','b',1
union all select '2013-3-5','c',5
select * from #tb
declare @sql varchar(8000)
set @sql=''
select @sql=@sql + ',['+rtrim([date])+']=max(case [date] when '''+rtrim([date])+''' then s1 end)'
from #tb group by [date]