我正在尝试查找数据库中出现次数最少的所有俱乐部,并显示该俱乐部名称及其出现的次数。以下是我尝试过的解决方案
select club, MIN(num)
from
(select count(club) as num
from member
order by club)
但是此代码引发错误:
SQL0119N在GROUP BY子句中未指定在SELECT子句,HAVING子句或ORDER BY子句中指定的以“ CLUB”开头的表达式,或者在带有列的SELECT子句,HAVING子句或ORDER BY子句中函数,未指定GROUP BY子句。
我是SQL新手-有人可以告诉我哪里出了问题以及如何解决此问题吗?
答案 0 :(得分:1)
您可以使用fetch first n rows only
语法:
SELECT club, COUNT(*)
FROM members
GROUP BY club
ORDER BY 2 ASC
FETCH FIRST 1 ROWS ONLY
答案 1 :(得分:0)
您正在朝着正确的方向前进。您需要进行两次汇总。
WITH Member_Count AS (SELECT club, COUNT(*) AS counted
FROM Members
GROUP BY club)
SELECT club, counted
FROM Member_Count
WHERE counted = (SELECT MIN(counted)
FROM Member_Count)