简体   繁体   中英

Mysql need to select all values having the biggest number of multiple entries

My mysql query

SELECT  count(ICD10) as columnDis, ICD10
FROM(
     SELECT tbl_disease.ICD10, tbl_symptom_disease.symptomCode 
     FROM `tbl_disease`
     JOIN tbl_symptom_disease
     ON tbl_disease.ICD10=tbl_symptom_disease.diseaseCode
     WHERE tbl_symptom_disease.symptomCode='P18' 
     OR tbl_symptom_disease.symptomCode='P19'
    ) AS tbl_ICD10_symCode
GROUP BY ICD10
HAVING columnDis=?????

So this query returns all the ICD10 entries having duplicate values equal with ?????.

In this example the maximum number of duplicates is 2, but I want to use it for different sympomCode(s) in which case there could be 3 duplicate entries that appear 5 times, 5 other ones that appear 3 times, 1 that appears 2 times and so on. So what I need is only the duplicate entries that appear the MOST times. Something like:

HAVING columnDis=(MAX(columnDis))

but that of course doesn't work. Can anyone help me please?

Using a sub query to get the max count, then joining that against the main sub query

SELECT ICD10
FROM
(
    SELECT MAX(disease_count) AS max_disease_count
    FROM
    (
        SELECT tbl_disease.ICD10, COUNT(tbl_disease.ICD10) AS disease_count
        FROM `tbl_disease`
        JOIN tbl_symptom_disease
        ON tbl_disease.ICD10=tbl_symptom_disease.diseaseCode
        WHERE tbl_symptom_disease.symptomCode='P18' 
        OR tbl_symptom_disease.symptomCode='P19'
        GROUP BY ICD10
    ) sub0
) sub1
INNER JOIN
(
    SELECT tbl_disease.ICD10, COUNT(tbl_disease.ICD10) AS disease_count
    FROM `tbl_disease`
    JOIN tbl_symptom_disease
    ON tbl_disease.ICD10=tbl_symptom_disease.diseaseCode
    WHERE tbl_symptom_disease.symptomCode='P18' 
    OR tbl_symptom_disease.symptomCode='P19'
    GROUP BY ICD10
) sub2
ON sub1.max_disease_count = disease_count

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM