合并两行的值以创建新行

时间:2015-08-05 07:06:47

标签: sql-server

1 个答案:

答案 0 :(得分:1)

请尝试以下查询: 我使用PIVOTROW_NUMBER OVER分别从行生成输入/输出值。请注意,用于确定输入/输出的任何业务逻辑都位于内部查询创建表s

select id,name, [1] as input, [2] as [Output] from 
(select id,name,destination,row_number() over (partition by id order by id) as colnum from tbl) s
pivot
(
 max(destination) for colnum in ([1],[2])
)p

示例sql小提琴链接:http://sqlfiddle.com/#!6/ed9f2/2