SQL - 显示计数最大值的条目?

发布于 2024-10-20 08:30:47 字数 445 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

Saygoodbye 2024-10-27 08:30:47

这应该可以做到。

SELECT docname, COUNT(*) FROM doctor GROUP BY name HAVING COUNT(*) = 
    (SELECT MAX(c) FROM
        (SELECT COUNT(patient) AS c
         FROM doctor
         GROUP BY docname))

另一方面,如果您只需要第一个条目,那么

SELECT docname, COUNT(docname) FROM doctor 
GROUP BY name 
ORDER BY COUNT(docname) DESC LIMIT 1;

This should do it.

SELECT docname, COUNT(*) FROM doctor GROUP BY name HAVING COUNT(*) = 
    (SELECT MAX(c) FROM
        (SELECT COUNT(patient) AS c
         FROM doctor
         GROUP BY docname))

On the other hand if you require only the first entry, then

SELECT docname, COUNT(docname) FROM doctor 
GROUP BY name 
ORDER BY COUNT(docname) DESC LIMIT 1;
标点 2024-10-27 08:30:47

这应该适合你:

SELECT docname
FROM doctor
GROUP BY docname
HAVING COUNT(patient)=
    (SELECT MAX(patientcount) FROM
        (SELECT docname,COUNT(patient) AS patientcount
         FROM doctor
         GROUP BY docname) t1)

This should do it for you:

SELECT docname
FROM doctor
GROUP BY docname
HAVING COUNT(patient)=
    (SELECT MAX(patientcount) FROM
        (SELECT docname,COUNT(patient) AS patientcount
         FROM doctor
         GROUP BY docname) t1)
蓝礼 2024-10-27 08:30:47

这是另一种选择,它只有一个子查询而不是两个:

SELECT docname
FROM author
GROUP BY name
HAVING COUNT(*) = (
    SELECT COUNT(*) AS c
    FROM author
    GROUP BY name
    ORDER BY c DESC
    LIMIT 1
)

Here's another alternative that only has one subquery instead of two:

SELECT docname
FROM author
GROUP BY name
HAVING COUNT(*) = (
    SELECT COUNT(*) AS c
    FROM author
    GROUP BY name
    ORDER BY c DESC
    LIMIT 1
)
薄凉少年不暖心 2024-10-27 08:30:47

由于您没有指定数据库产品或版本,因此允许任何 ISO SQL 规范中的任何功能,并假设患者表称为“患者”并具有名为“docname”的列,以下内容可能会为您提供您想要的内容:

With PatientCounts As
    (
    Select docname
        , Count(*) As PatientCount
    From patient
    Group By docname
    )
    , RankedCounts As
    (
    Select docname, PatientCount
        , Rank() Over( Order By PatientCount ) As PatientCountRank
    From PatientCounts
    )
Select docname, PatientCount, PatientCountRank
From RankedCounts 
Where PatientCountRank = 1

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:

With PatientCounts As
    (
    Select docname
        , Count(*) As PatientCount
    From patient
    Group By docname
    )
    , RankedCounts As
    (
    Select docname, PatientCount
        , Rank() Over( Order By PatientCount ) As PatientCountRank
    From PatientCounts
    )
Select docname, PatientCount, PatientCountRank
From RankedCounts 
Where PatientCountRank = 1
愿与i 2024-10-27 08:30:47

当使用 ... HAVING COUNT(*) = ( ...MAX().. ) 有效时:

  • 在查询中,它需要几乎相同的子查询两次。
  • 对于大多数数据库,它需要一个二级子查询 MAX( COUNT(*) )
    不支持。

虽然使用 TOP / LIMIT / RANK 等有效:

  • 它使用特定数据库的 SQL 扩展。

另外,使用 TOP / LIMIT 为 1 只会给出一行 - 如果有两个或更多医生具有相同的最大患者数怎么办?

我会将问题分解为几个步骤:

获取目标字段和关联的计数

SELECT docName, COUNT( patient ) AS countX
FROM doctor
GROUP BY docName

使用上面的内容作为 CTE(语句范围视图),使用它来获取最大计数行( s)

WITH x AS
(
    SELECT docName, COUNT( patient ) AS countX
    FROM doctor
    GROUP BY docName
)
SELECT x.docName, x.countX
FROM x
WHERE x.countX = ( SELECT MAX( countX ) FROM x )

WITH 子句定义了 CTE,有效地提供了可以在同一查询中重复使用的命名子查询。

虽然这个使用 CTE 的解决方案较长,但它:

  • 更容易测试
  • 自记录
  • 可扩展

测试更容易,因为查询的一部分可以独立运行。

它是自记录的,因为查询直接反映了需求
即 CTE 列出目标字段和相关计数。

它是可扩展的,就像需要其他条件或字段一样,可以轻松地将其添加到 CTE。
例如,在这种情况下,应该更改表结构以包含医生 ID 作为主键字段,并且这应该是结果的一部分。

While using ... HAVING COUNT(*) = ( ...MAX().. ) works:

  • Within the query, it needs almost the same sub-query twice.
  • For most databases, it needs a 2nd level sub-query as MAX( COUNT(*) )
    is not supported.

While using TOP / LIMIT / RANK etc works:

  • It uses SQL extensions for a specific database.

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

SELECT docName, COUNT( patient ) AS countX
FROM doctor
GROUP BY docName

Using the above as a CTE, (a statement scoped view), use this to get the max count row(s)

WITH x AS
(
    SELECT docName, COUNT( patient ) AS countX
    FROM doctor
    GROUP BY docName
)
SELECT x.docName, x.countX
FROM x
WHERE x.countX = ( SELECT MAX( countX ) FROM x )

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:

  • Easier to test
  • Self documenting
  • Extendable

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.

第七度阳光i 2024-10-27 08:30:47

获取两个查询并将它们连接在一起以获得最大值:

 SELECT
      docName,
      m.MaxCount
    FROM
      author
    INNER JOIN
     (
      SELECT 
            MAX(count)  as MaxCount,
            docName
      FROM 
            (SELECT 
                  COUNT(docname) 
             FROM 
                  doctor 
             GROUP BY 
                  docname
            )
      ) m ON m.DocName = author.DocName 

Take both queries and join them together to get the max:

 SELECT
      docName,
      m.MaxCount
    FROM
      author
    INNER JOIN
     (
      SELECT 
            MAX(count)  as MaxCount,
            docName
      FROM 
            (SELECT 
                  COUNT(docname) 
             FROM 
                  doctor 
             GROUP BY 
                  docname
            )
      ) m ON m.DocName = author.DocName 
吾家有女初长成 2024-10-27 08:30:47

另一种使用 CTE 的替代方案:

with cte_DocPatients
as
(
select docname, count(*) as patientCount
from doctor
group by docname
)
select docname, patientCount from 
cte_DocPatients where
patientCount = (select max(patientCount) from cte_DocPatients)

Another alternative using CTE:

with cte_DocPatients
as
(
select docname, count(*) as patientCount
from doctor
group by docname
)
select docname, patientCount from 
cte_DocPatients where
patientCount = (select max(patientCount) from cte_DocPatients)
如梦初醒的夏天 2024-10-27 08:30:47

如果您不需要关心性能,我认为只需排序并选择第一个元素即可。像这样的事情:

SELECT docname, COUNT(docname) as CNT 
FROM doctor 
WHERE docname = docname 
GROUP BY docname
ORDER BY CNT DESC
LIMIT 1

if you do not need to care about performance I think just sorting and pick first element. Something like this:

SELECT docname, COUNT(docname) as CNT 
FROM doctor 
WHERE docname = docname 
GROUP BY docname
ORDER BY CNT DESC
LIMIT 1
白龙吟 2024-10-27 08:30:47

这将为您提供每位医生的姓名以及各自治疗患者的数量

SELECT docname, COUNT(docname) as TreatingPatients FROM doctor
WHERE docname = docname
GROUP BY docname

This will give you each doctor name and respective count of treating patients

SELECT docname, COUNT(docname) as TreatingPatients FROM doctor
WHERE docname = docname
GROUP BY docname
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文