简体   繁体   English

SQL Server 2008 r2如何将列值拆分为多行

[英]SQL Server 2008 r2 how to split column values to multiple rows

How to split column values to raw values eg: 如何将列值拆分为原始值,例如:

Col1            col2
1               james
1               gold
1               james@gmail.com
2               john
2               def
2               def@gmail.com

col1 values keep on changing to thousands. col1值不断变化到数千。

results: 结果:

col1            col2      col3      col4
1               james     gold      james@gmail.com 
2               john      def       ef@gmail.com

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 这不保证列之间值的顺序,因为表中没有列来显示列的顺序。如果你有一个列,就像那样,你可以改变那个列的选择0

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 SQL小提琴演示

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

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