我有以下两个表:STUDENTS, SUBJECTS
**STUDENTS**
ID NAME SUBJECT
1 JOHN A
2 JANE A
3 TOM B
4 TIM C
5 CHRI C
6 APE C
**SUBJECTS**
ID NAME
1 A
2 B
3 C在上面的学生表中,我有A repeated 2 times, B once (this is minimum) and C - 3 times(This is maximum)
如何编写只获取不是最大值也不是最小值的主题名称的查询。
在上面的示例数据中,我的输出应该是A。
发布于 2019-09-13 04:36:33
您可以使用窗口函数:
select subject
from (select subject, count(*) as cnt,
max(count(*)) over () as max_cnt,
min(count(*)) over () as min_cnt
from t
group by subject
) s
where cnt not in (max_cnt, min_cnt);发布于 2019-09-13 04:48:55
另一种方法:
SELECT SUBJECT
FROM STUDENTS
GROUP BY SUBJECT
HAVING COUNT(*) > 1 AND COUNT(*) < 3https://stackoverflow.com/questions/57914129
复制相似问题