查找成员(患者)人数最多的保险公司。
这就是数据库,也是我第一次尝试靠近:
http://sqlfiddle.com/#!3/4b2ed/27
SELECT P.cid AS cid, COUNT ( P.pid ) AS CountP
FROM Patient P
GROUP BY P.cid
我如何获得最大计数?
查找成员(患者)人数最多的保险公司。
这就是数据库,也是我第一次尝试靠近:
http://sqlfiddle.com/#!3/4b2ed/27
SELECT P.cid AS cid, COUNT ( P.pid ) AS CountP
FROM Patient P
GROUP BY P.cid
我如何获得最大计数?
只需按患者数量递减的顺序排列您的结果,并且只记录第一个记录。
SELECT P.cid AS cid, COUNT( P.pid ) AS CountP
FROM Patient P
GROUP BY P.cid
ORDER BY CountP DESC
LIMIT 1
如果不止一家保险公司可能拥有最大数量的成员,并且您想获取所有成员,则需要使用子查询:
SELECT P.cid AS cid, COUNT( P.pid ) AS CountP
FROM Patient P
GROUP BY P.cid
HAVING CountP = (
SELECT COUNT(pid) AS c
FROM Patient
GROUP BY cid
ORDER BY c DESC
LIMIT 1
);
在您在评论中描述的奇怪情况下,无法使用LIMIT
,您可以这样做:
SELECT P.cid AS cid, COUNT( P.pid ) AS CountP
FROM Patient P
GROUP BY P.cid
HAVING CountP = (SELECT MAX(d.c) FROM (
SELECT COUNT(pid) AS c
FROM Patient
GROUP BY cid
) AS d);
尝试以下操作:
max(count(P.pid))
SELECT P.cid, COUNT ( P.pid ) AS CountP
FROM Patient P
GROUP BY P.cid
compute max(count(p.pid))
哇 - 很多限制。干得好:
SELECT MAX(CountP) FROM (
SELECT P.cid, COUNT ( P.pid ) AS CountP
FROM Patient P
GROUP BY P.cid
) winner