sql语句如何合并相同id下的多行数据在一行 例如: 30
以id为基准,其他数据根据id合并为一行,其中id数量较大,不能枚举源数据idopresultelse1As322Bf233Cs563As222Af2数据结果idop_1...
以id为基准,其他数据根据id合并为一行,其中id数量较大,不能枚举
源数据idopresultelse1As322Bf233Cs563As222Af2数据结果idop_1(=A)result_1else_1op_2(=B)result_2op_3(=c)result_31As32nullnullnullnull2Af2Bfnullnull3As22nullnullCs 展开
源数据idopresultelse1As322Bf233Cs563As222Af2数据结果idop_1(=A)result_1else_1op_2(=B)result_2op_3(=c)result_31As32nullnullnullnull2Af2Bfnullnull3As22nullnullCs 展开
2个回答
展开全部
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;
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;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询