SQL行转成列的语句

SELECT [title]
      ,[color]
      ,[sign]
  FROM [dentistry].[dbo].[TestColor]

declare @sql nvarchar(1000)
set @sql = 'select title'

select @sql = @sql + ',sum(case color when ''' + color + ''' then sign else 0 end) ['+color+']'
from (select distinct color from TestColor) as a

select @sql = @sql + ' from TestColor group by title'

print @sql

exec (@sql)

猜你喜欢

转载自blog.csdn.net/seamonkey/article/details/4577279