所以这是问题
查找目的地飞行距离超过同一目的地所有航班平均距离的飞机的ID
这是架构
目标(已:整数,dname:字符串,状态:字符串)
飞机(援助:整数,aname:字符串,颜色:字符串)
航班(援助:整数,确实:整数,fdate:日期,距离:真实)
到目前为止,我已经能够找到此查询的两个不同部分的答案。
我找到了飞往每个目的地的平均距离。 我还发现每架飞机到达每个位置的总距离。
上面的查询如下:
SELECT D.DID, AVG(F.DIST) as AvgDist from Destinations D join Flights F ON F.DID = D.DID GROUP BY D.DID;
SELECT F.AID,F.DID, SUM(F.DIST) as fDist from Flights F join Destinations D ON F.DID = D.DID GROUP BY F.AID,F.DID;
我的问题是我没有看到如何将它们结合起来找到解决方案。我知道我需要做的就是找出一种方法来检查该目的地的平均距离是否小于该飞机飞行的距离,但我不知道该怎么办。我所有的条款似乎都不起作用所以我现在有点困惑。
答案 0 :(得分:1)
分析功能并不困难:
SELECT DISTINCT AID FROM (
SELECT F.AID, D.DID,
AVG(F.DIST) OVER (PARTITION BY D.DID) as AvgDist,
SUM(F.DIST) OVER (PARTITION BY F.AID, D.DID) as AirDistance
FROM Destinations D join Flights F ON F.DID = D.DID
) WHERE AirDistance > AvgDist;
使用子查询,它应该是这样的:
SELECT DISTINCT a.AID
FROM
(SELECT D.DID, AVG(F.DIST) as AvgDist
FROM Destinations D join Flights F
ON F.DID = D.DID GROUP BY D.DID
) d
JOIN
(SELECT F.AID,F.DID, SUM(F.DIST) as fDist
FROM Flights F join Destinations D
ON F.DID = D.DID GROUP BY F.AID,F.DID
) a
ON d.DID = a.DID
WHERE fDist > AvgDist;