行列的转换
下面是我表的结构和数据
PK_Param FK_Point PlanDate
1 1 2007-05-24
2 1 2007-05-24
3 1 2007-06-22
4 1 2007-06-22
5 1 2007-06-22
根据这些数据.要得到下面的结果
PK_Param FK_Point PlanDate
1,2 1 2007-05-24
3,4,5 1 2007-06-22
------解决方案--------------------declare @sql varchar(8000)
set @sql = 'select PlanDate,FK_Point
select @sql = @sql + ' , sum(case PK_Param when ' ' ' + PK_Param + ' ' ' then PK_Param else 0 end) [ ' + PK_Param + '] '
from (select distinct PK_Param from tb) as a
set @sql = @sql + ' ,sum(PK_Param) 合计 from tb group by PlanDate,FK_Point
exec(@sql)