SQL数据透视表

2020年11月4日 32点热度 0条评论 来源: Li Yuan

enter image description hereSQL数据透视表

请参阅上图中我的SQL问题。

如何为此编写SQL?


===========解决方案如下:

DECLARE @Columns nvarchar(MAX),@SQL nvarchar(MAX); 
SET @Columns=N''; 
SELECT @Columns+=IIF(@Columns='', 
QUOTENAME(continent),N','+QUOTENAME(continent)) 
from(Select continent from student group by continent) as x ; 
Set @SQL=N'SELECT ['+STUFF(@Columns,1,1,'')+' 
FROM (Select continent,name,row_number() over(partition by continent order 
by name) rn from student) AS j 
PIVOT(MAX(name) FOR continent IN (['+STUFF(@Columns,1,1,'')+')) as p'; 
EXECUTE sp_executesql @SQL; 
    原文作者:Li Yuan
    原文地址: https://stackoverflow.com/q/44748495
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。