我的表格看起来像这样:
FlightLeg
+------+-----+-------+------+---------------------+---------------------+-------+
| FLNO | Seq | FromA | ToA | DeptTime | ArrTime | Plane |
+------+-----+-------+------+---------------------+---------------------+-------+
| 1000 | 1 | DFW | LOG | 2020-01-01 01:10:20 | 2020-01-01 01:13:40 | 7 |
| 1010 | 1 | LAX | ORD | 2020-01-01 01:13:10 | 2020-01-01 01:16:20 | 3 |
| 1020 | 1 | LOG | JFK | 2020-01-01 01:05:40 | 2020-01-01 01:06:20 | 9 |
| 1030 | 1 | LAX | INT | 2020-01-01 01:11:20 | 2020-01-01 01:16:10 | 6 |
| 1040 | 1 | LAX | LGA | 2020-02-01 01:15:30 | 2020-01-01 01:21:00 | 1 |
| 1010 | 2 | ORD | JFK | 2020-01-01 01:17:10 | 2020-01-01 01:20:20 | 3 |
| 1020 | 2 | JFK | DFW | 2020-01-01 01:07:20 | 2020-01-01 01:10:20 | 9 |
| 1030 | 2 | INT | DFW | 2020-01-01 01:17:20 | 2020-01-01 01:18:00 | 6 |
| 1020 | 3 | DFW | INT | 2020-01-01 01:11:10 | 2020-01-01 01:11:40 | 7 |
| 1020 | 4 | INT | LAX | 2020-01-01 01:12:20 | 2020-01-01 01:15:10 | 7 |
+------+-----+-------+------+---------------------+---------------------+-------+
而且:
飞行
+------+--------+---------+
| FLNO | Meal | Smoking |
+------+--------+---------+
| 1000 | Bistro | Y |
| 1010 | Meal | N |
| 1020 | Meal | Y |
| 1030 | Snack | N |
| 1040 | Meal | N |
+------+--------+---------+
我试图写一个查询,其中每个航班有两条腿以上,检索航班号(FLNO),第一个出发城市(FromA)和最后到达的城市。飞行序列可以从一个任意整数开始,并可以用任意整数偏移量分开。
这是我到目前为止所做的:
SELECT FlightLeg.FLNO, (
SELECT FlightLeg.FromA
FROM FlightLeg
INNER JOIN Flight
ON FlightLeg.FLNO=Flight.FLNO
GROUP BY FlightLeg.FLNO
HAVING COUNT(*) > 2),
(
SELECT FlightLeg.ToA
FROM FlightLeg
INNER JOIN Flight
ON FlightLeg.FLNO=Flight.FLNO
GROUP BY FlightLeg.FLNO
HAVING COUNT(*) > 2)
FROM FlightLeg
INNER JOIN Flight
ON FlightLeg.FLNO=Flight.FLNO
GROUP BY FlightLeg.FLNO
HAVING COUNT(*) > 2;
所有我能得到的输出是FLNO,FromA,正确航班的第一序列的ToA。
预期产出:
+------+-------+------+
| FLNO | FromA | ToA |
+------+-------+------+
| 1020 | LOG | LAX |
+------+-------+------+
我认为我非常接近,但我不确定如何找到FromA,最小序列和最大序列的ToA。
有人可以帮忙吗?谢谢!
答案 0 :(得分:2)
解决此问题的一种方法是使用自连接和派生表,如下所示:
select flight_start.flno, flight_start.froma, flight_end.toa
from flightleg flight_start
join flightleg flight_end on flight_start.flno = flight_end.flno
join (
select
flno,
min(seq) origin,
max(seq) destination
from flightleg
group by flno
having count(*) > 2
) min_max_seq
on flight_start.flno = min_max_seq.flno
and flight_start.seq = min_max_seq.origin
and flight_end.seq = min_max_seq.destination
用作派生表的查询确定每个航班的最低和最高序号,第一个联接使用较低序列号来检索原始机场,而第二个联接使用较高序列号来检索目的地机场。腿数由派生表中的having
子句确定。
在旁注中:如果序列号不是订单的一个好的决定因素,那么您可以使用日期来确定订单或订单。
答案 1 :(得分:2)
select x.flno,
Max( if( y.DeptTime = x.starter, y.FromA, null )) FirstLegFrom,
Max( if( y.DeptTime = x.starter, y.DeptTime, null )) FirstLegStart,
Max( if( y.DeptTime = x.ender, y.ToA, null )) LastLegFrom,
Max( if( y.DeptTime = x.ender, y.ArrTime, null )) LastLegArrival
from(
select flno, min(depttime) as starter, max(depttime) as ender
from flightleg
group by flno
having count(*) > 2
) x
join flightleg y
on x.flno = y.flno
and (x.starter = y.depttime
or x.ender = y.depttime)
group by x.flno;
小提琴:http://sqlfiddle.com/#!9/79640/1/0 (由TommCatt提议进行修改以提高效率)
这不依赖于seq
每个航班的升序。