答案 0 :(得分:0)
假设您最多有两行,则可以使用row_number()
来枚举值和条件聚合(或根据需要进行透视):
select m.movementid, m.arrflt, m.depflt,
sum(case when seqnum = 1 then des else 0 end) as des_1,
sum(case when seqnum = 1 then cargo else 0 end) as cargo_1,
sum(case when seqnum = 1 then mail else 0 end) as mail_1,
sum(case when seqnum = 1 then luggage else 0 end) as luggage_1,
sum(case when seqnum = 2 then des else 0 end) as des_2,
sum(case when seqnum = 2 then cargo else 0 end) as cargo_2,
sum(case when seqnum = 2 then mail else 0 end) as mail_2,
sum(case when seqnum = 2 then luggage else 0 end) as luggage_2
from movement m join
(select md.*,
row_number() over (partition by movementid order by movementid) as seqnum
from movementdetail md
) md
on md.movementid = m.movementid
group by m.movementid, m.arrflt, m.depflt;