如何配对到达和离开的行

时间:2014-02-07 01:58:01

标签: sql-server

FPL_ID  AFSKEY  FLIGHTNO FLIGHTYPE  STAD                   AIRCRAFTTYPECODE TAILNO STANDCODE

1733285 4383931 UL 0314   A        2014-01-01 05:35:00.000  A343             4RADC  C015
1733554 4382525 UL 0315   D        2014-01-01 08:25:00.000  A343             4RADC  C015
1733385 4382929 AK 5107   A    2014-01-01 07:00:00.000  A320         9MAFB  F086
1733484 4381571 AK 5212   D    2014-01-01 07:25:00.000  A320         9MAFB  F086

我需要帮助。 如何基于FLIGHTYPE A = Arrival和D = Departure配对成一行?

1 个答案:

答案 0 :(得分:1)

再次内部加入同一个表。在下面的示例中,f1将包含到达和f2离开。

select f1.*, f2.*  -- replace with the list of columns you need
from flights f1
inner join flighs f2 
    on f1.FLIGHTNO = f2.FLIGHTNO
    and f1.FPL_ID <> f2.FPL_ID
    and f1.FLIGHTYPE = 'A' and f2.FLIGHTYPE = 'D'