以下SQL查询的正确解决方案是什么?

问题描述 投票:3回答:3

Print name of all activities with neither maximum nor minimum number of participants

我已经尝试过以下查询,但给我错误:

select ACTIVITY
from (select ACTIVITY, count(*) as cnt,
             max(count(*)) as max_cnt,
             min(count(*)) as min_cnt
      from FRIENDS GROUP BY ACTIVITY) FRIENDS
where cnt not in (max_cnt, min_cnt);

错误:第1行的错误1111(HY000):无效使用组功能MYSQL版本:8

mysql sql group-by count greatest-n-per-group
3个回答
1
投票

大概,您没有运行MySQL 8.0(否则,您上一个问题给出的答案将是可行的。)>

在较早的版本中,您可以这样做:

select activity, count(*) no_activities
from friends
group by activity
having 
        count(*) > (select count(*) from friends group by activity order by count(*)  asc limit 1)
    and count(*) < (select count(*) from friends group by activity order by count(*) desc limit 1)

0
投票

尝试以下操作,它应该使用MySQL 8.0中的窗口功能。


0
投票

您想要窗口功能:

© www.soinside.com 2019 - 2024. All rights reserved.