pgsql的一句经典代码
select mp3.* from (
select distinct (t1.flight_number) as flight_number1,items.* from guarantee_node t1
left join LATERAL (
select * from guarantee_node gn where t1.flight_number=gn.flight_number
order by guarantee_time desc limit 1
) AS items ON true
union all
select distinct (t1.flight_number) as flight_number,items.* from guarantee_node t1
left join LATERAL (
select * from guarantee_node gn where t1.flight_number=gn.flight_number
order by guarantee_time asc limit 1
) AS items ON true
) mp3
where
mp3.flight_number1 in (
select flight_number from guarantee_node where
guarantee_type='航空器离位'
)
order by mp3.flight_number1 asc ,guarantee_time desc
漫思