简体   繁体   English

SQL Server 2005数据透视表

[英]SQL Server 2005 pivot table

I want to pivot a column in SQL Server 2005. I'm pretty sure there is XML way to get it done but can't figure it out. 我想在SQL Server 2005中透视一列。我非常确定有XML方法可以完成它,但无法弄清楚。 Here is a table: 这是一张桌子:

ID    Class
1     20002
1     20003
1     20004
2     20003
2     20012

Desired value is: 所需值为:

ID    Class
1     20002,20003,20004
2     20003,20012

Thanks in advance 提前致谢

I am not sure PIVOT (or UNPIVOT) are what you are looking for. 我不确定PIVOT(或UNPIVOT)是否在找您。 Below is some code that I use when I need to get a CSV list embedded in a query. 以下是一些我需要在查询中嵌入CSV列表时使用的代码。 Hope it helps! 希望能帮助到你!

SELECT DISTINCT 
        ID
      , Class = STUFF(
                       cast(
                            (select ', ' + cast(Class as nvarchar) 
                             from TableName t2 
                             WHERE t2.ID = t1.ID 
                             for xml path('')) as nvarchar(2000))
                ,1,2, N'')
FROM TableName t1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM