SQL select 所有 ID 均高于平均水平

SQL select the all ID's above average

我有一个名为 flights 的 table 列:

DEP_DELAY, ID  

我想 select 所有高于 DEP_DELAY 平均值的 ID 并将这些 ID 加在一起,以了解有多少 ID 高于平均值 DEP_DELAY。

我试过这个:

select avg(DEP_DELAY), count(ID)
from flights
group by DEP_DELAY
having DEP_DELAY > (select avg(DEP_DELAY) from flights)

但是这个不行,如果有任何建议,我将不胜感激。

嗯。 . .您希望 having 子句作为 where 子句。并且在外部查询中没有 group by 因为您只需要一行:

select avg(f.DEP_DELAY), count(*)
from flights f
where f.DEP_DELAY > (select avg(f2.DEP_DELAY) from flights f2);