关于列转行的一点点心德 SQL
ALTER proc so_total_report
@columnName varchar(10)
as
declare @sql varchar(8000)
set @sql = 'select '+@columnName+''
select @sql = @sql + ',SUM(case td when ''' + td + ''' then sar else 0 end) [' + td + '下]'
+',SUM(case td when ''' + td + ''' then sina else 0 end) [' + td + '入]'
+',SUM(case td when ''' + td + ''' then snoa else 0 end) [' + td + '未]'
from (select distinct td from so_jiaoqi) as a
set @sql = @sql + ' from so_jiaoqi group by '+@columnName
print @sql
exec(@sql)
EXEC so_total_report 'tp'
?
?
?