CREATE TABLE #tempcloum(
[productNum] varchar(20)
null,
[year1] decimal not
null,
[year2] decimal not
null,
[year3] decimal not
null
)
--
лляD┴л
DECLARE @sql VARCHAR(800)
SET @sql =
'SELECT [productNum],'
SELECT @sql = @sql +
'SUM(CASE [year] WHEN '''+[year]+
''' THEN [price] ELSE 0 END) AS '''+[year]+
''','
FROM (SELECT DISTINCT [year] FROM #tempYear) AS a
SELECT @sql = LEFT(@sql,LEN(@sql)-
1) +
' FROM #tempYear GROUP BY [productNum] '
PRINT(@sql)
insert into #tempcloum
EXEC(@sql)
GO
转载于:https://www.cnblogs.com/Mr-Worlf/p/8709223.html
转载请注明原文地址: https://mac.8miu.com/read-17478.html