SQL查询中分组后的平均值

时间:2021-05-20 20:54:09

标签: mysql sql h2

select avg(select count(aid)
           from athlete
           group by codepays)

我收到“不止一行错误”。 我如何从我的拳头选择中获得结果的平均值?

2 个答案:

答案 0 :(得分:1)

您需要使用表表达式(子查询)。

例如:

select avg(cnt)
from (
  select count(aid) as cnt
  from athlete
  group by codepays
) x

答案 1 :(得分:0)

您可以使用除法来做到这一点:

select count(aid) / count(distinct codepay)
from athlete;

不需要子查询。

(尽管如果 codepay 实际上可以是 NULL,则需要调整算术。)

相关问题