SQL - 显示计数最大值的条目?
CREATE TABLE doctor( patient CHAR(13), docname CHAR(30) );
假设我有一个这样的表,那么我如何显示病人最多的医生的名字呢?就像如果最多是三个并且两个医生有三个病人,那么我会显示他们两个的名字。
这将获得最大患者数:
SELECT MAX(count)
FROM (SELECT COUNT(docname) FROM doctor GROUP BY docname) a;
这是所有医生以及他们有多少患者:
SELECT docname, COUNT(docname) FROM doctor GROUP BY name;
现在我不知道如何将它们组合起来以仅列出拥有最大患者数的医生的姓名。
谢谢。
CREATE TABLE doctor( patient CHAR(13), docname CHAR(30) );
Say I had a table like this, then how would I display the names of the doctors that have the most patients? Like if the most was three and two doctors had three patients then I would display both of their names.
This would get the max patients:
SELECT MAX(count)
FROM (SELECT COUNT(docname) FROM doctor GROUP BY docname) a;
This is all the doctors and how many patients they have:
SELECT docname, COUNT(docname) FROM doctor GROUP BY name;
Now I can't figure out how to combine them to list only the names of doctors who have the max patients.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这应该可以做到。
另一方面,如果您只需要第一个条目,那么
This should do it.
On the other hand if you require only the first entry, then
这应该适合你:
This should do it for you:
这是另一种选择,它只有一个子查询而不是两个:
Here's another alternative that only has one subquery instead of two:
由于您没有指定数据库产品或版本,因此允许任何 ISO SQL 规范中的任何功能,并假设患者表称为“患者”并具有名为“docname”的列,以下内容可能会为您提供您想要的内容:
Allowing for any feature in any ISO SQL specification since you did not specify a database product or version, and assuming that the table of patients is called "patients" and has a column called "docname", the following might give you what you wanted:
当使用 ... HAVING COUNT(*) = ( ...MAX().. ) 有效时:
不支持。
虽然使用 TOP / LIMIT / RANK 等有效:
另外,使用 TOP / LIMIT 为 1 只会给出一行 - 如果有两个或更多医生具有相同的最大患者数怎么办?
我会将问题分解为几个步骤:
获取目标字段和关联的计数
使用上面的内容作为 CTE(语句范围视图),使用它来获取最大计数行( s)
WITH 子句定义了 CTE,有效地提供了可以在同一查询中重复使用的命名子查询。
虽然这个使用 CTE 的解决方案较长,但它:
测试更容易,因为查询的一部分可以独立运行。
它是自记录的,因为查询直接反映了需求
即 CTE 列出目标字段和相关计数。
它是可扩展的,就像需要其他条件或字段一样,可以轻松地将其添加到 CTE。
例如,在这种情况下,应该更改表结构以包含医生 ID 作为主键字段,并且这应该是结果的一部分。
While using ... HAVING COUNT(*) = ( ...MAX().. ) works:
is not supported.
While using TOP / LIMIT / RANK etc works:
Also, using TOP / LIMIT of 1 will only give one row - what if there are two or more doctors with the same maximum number of patients?
I would break the problem into steps:
Get target field(s) and associated count
Using the above as a CTE, (a statement scoped view), use this to get the max count row(s)
The WITH clause, which defines a CTE, effectively gives named sub-queries that can be re-used within the same query.
While this solution, using CTEs, is longer, it is:
It is easier to test as parts of the query can be run standalone.
It is self documenting as the query directly reflects the requirement
ie the CTE lists the target field(s) and associated count.
It is extendable as if other conditions or fields are required, this can be easily added to the CTE.
eg in this case, the table structure should be changed to include a doctor-id as a primary key field and this should be part of the results.
获取两个查询并将它们连接在一起以获得最大值:
Take both queries and join them together to get the max:
另一种使用 CTE 的替代方案:
Another alternative using CTE:
如果您不需要关心性能,我认为只需排序并选择第一个元素即可。像这样的事情:
if you do not need to care about performance I think just sorting and pick first element. Something like this:
这将为您提供每位医生的姓名以及各自治疗患者的数量
This will give you each doctor name and respective count of treating patients