sql语句如何合并相同id下的多行数据在一行 例如:

以id为基准,其他数据根据id合并为一行,其中id数量较大,不能枚举
源数据idopresultelse1As322Bf233Cs563As222Af2数据结果idop_1(=A)result_1else_1op_2(=B)result_2op_3(=c)result_31As32nullnullnullnull2Af2Bfnullnull3As22nullnullCs

第1个回答  2013-05-07
select id,
max(case when rn = 1 then op end ) op_1,
max(case wehn rn = 1 then result end ) result_1,
max(case when rn = 1 then else end ) else_1,
max(case when rn = 2 then op end ) op_2,
max(case wehn rn = 2 then result end ) result_2,
max(case when rn = 2 then else end ) else_2
from (
select a.*,
row_number() over(partition by id order by else) rn
from a)
group by id;
第2个回答  2013-05-09

相似回答