SQL - Displaying entries that are the max of a count?

后端 未结 7 1441
悲&欢浪女
悲&欢浪女 2020-12-01 12:35
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

7条回答
  •  日久生厌
    2020-12-01 13:24

    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
    

提交回复
热议问题