当前位置: 动力学知识库 > 问答 > 编程问答 >

SQL Server 2008 r2 how to split column values to multiple rows

问题描述:

How to split column values to raw values e.g:

Col1 col2

1 james

1 gold

1 [email protected]

2 john

2 def

2 [email protected]

col1 values keep on changing to thousands.

results:

col1 col2 col3 col4

1 james gold [email protected]

2 john def [email protected]

网友答案:

try this:

This will not guarantee the order of the values among the columns, as the table doesnt have a column to show the order of the columns..If you have a column, like that, you could change select 0 with that column

with cte as(
select *,ROW_NUMBER() 
         over(partition by Col1 order by (select 0))as rn from Table1)  
select * from cte
pivot(
   Max(COL2) FOR rn IN ([1],[2],[3] )  
   )P 


SQL fiddle demo

分享给朋友:
您可能感兴趣的文章:
随机阅读: