SQL:加入组函数

时间:2017-03-23 17:31:00

标签: mysql sql join sum max

我的表是:

表1

Uid,hospital_nr,department_nr,diagnostic_nr,case_amount

我的查询: 我想知道,对于每家医院而言,哪个部门的病例最多为三个特定诊断。

要找出这些诊断的每个部门的案例总数,我使用:

SELECT hospital_nr, department_nr, sum(case_amount) AS cases 
FROM Table_1 
WHERE diagnosis_nr = 1 OR diagnosis_nr = 3 OR diagnosis_nr = 4
GROUP BY hospital_nr, department_nr;

要了解部门对各个医院的最大案例数,我使用:

SELECT b.hospital_nr, max(a.sum_of_cases) AS max_sum_of_cases
FROM hdiag_data2014 AS b,
(SELECT Hospital_nr, department_nr, sum(case_amount) AS sum_of_cases 
FROM Table_1 
WHERE diagnosis_nr = 1 OR diagnosis_nr = 3 OR diagnosis_nr = 4
GROUP BY hospital_nr, department_nr) AS a
WHERE diagnosis_nr = 1 OR diagnosis_nr = 3 OR diagnosis_nr = 4
AND b.hospital_nr = a.hospital_nr 
GROUP BY b.hospital_nr;

现在我想在INNER JOIN中加入这两个表并尝试过这个:

SELECT c.hospital_nr, c.department_nr, sum(case_amount) AS cases 
FROM Table_1 AS c
INNER JOIN
    (SELECT b.hospital_nr max(a.sum_of_cases) AS max_sum_of_cases
    FROM Table_1 AS b,
        (SELECT hospital_nr, department_nr, sum(case_amount) AS sum_of_cases
        FROM Table_1 
        WHERE diagnosis_nr = 1 OR diagnosis_nr = 3 OR diagnosis_nr = 4
    GROUP BY hospital_nr, department_nr) AS a
    WHERE b.diagnosis_nr = 1 OR b.diagnosis_nr = 3 OR b.diagnosis_nr = 4
    AND b.hospital_nr = a.hospital_nr 
    GROUP BY b.hospital_nr) AS b 
ON c.cases = b.max_sum_of_cases
WHERE c.diagnosis_nr = 1 OR c.diagnosis_nr = 3 OR c.diagnosis_nr = 4
GROUP BY c.hospital_nr;

此脚本不允许我通过此ON加入,因为它表示它不会将“案例”识别为列。这是为什么?我怎样才能改进它? 我回答我的“口头查询”的第一条路径是通过HAVING条款工作,但这也是不成功的,因为它不允许我按最大案例数量的部门进行过滤。我忽略了这条替代路径中的某些东西吗?

3 个答案:

答案 0 :(得分:0)

我建议使用substring_index() / group_concat()诀窍:

SELECT hospital_nr,
       SUBSTRING_INDEX(GROUP_CONCAT(department_nr ORDER BY cases DESC), ',', 1) as max_department_nr
FROM (SELECT hospital_nr, department_nr, sum(case_amount) AS cases 
      FROM Table_1 
      WHERE diagnosis_nr in (1, 3, 4)
      GROUP BY hospital_nr, department_nr
     ) hd
GROUP BY hospital_nr;

还有其他方法,但这种方法通常是MySQL中最简单的方法。

注意:这假定department_nr不包含逗号。

答案 1 :(得分:0)

我认为你错过了别名' c。'在加入。其他一些别名也丢失了,所以我不确定他们应该从哪个表中提取。

SELECT c.hospital_nr, c.department_nr, c.cases 
FROM (SELECT hospital_nr, department_nr, sum(case_amount) AS cases
        FROM Table_1 
        WHERE diagnosis_nr in (1, 3, 4)
    GROUP BY hospital_nr, department_nr) AS c
INNER JOIN
    ((SELECT b.hospital_nr, max(a.sum_of_cases) AS max_sum_of_cases
    FROM hdiag_data2014) AS b,
        (SELECT hospital_nr, department_nr, sum(case_amount) AS sum_of_cases
        FROM Table_1 
        WHERE diagnosis_nr in (1, 3, 4)
    GROUP BY hospital_nr, department_nr) AS a
    WHERE b.diagnosis_nr in (1, 3, 4)
    AND b.hospital_nr = a.hospital_nr 
    GROUP BY b.hospital_nr) AS b 
ON c.cases = b.max_sum_of_cases);

答案 2 :(得分:0)

您可以在WHERE子句中使用带有LIMIT 1的有序相关子查询来过滤department_nr,其中case_amount的总和最高:

SELECT DISTICT hospital_nr, department_nr
FROM Table_1 t1
WHERE department_nr = (
    SELECT department_nr 
    FROM Table_1 t2
    WHERE t2.hospital_nr = t1.hospital_nr
      AND t2.diagnosis_nr IN (1, 3, 4)
    ORDER BY sum(case_amount) DESC
    LIMIT 1
)

如果您还需要总和,则需要再次计算:

SELECT hospital_nr, department_nr, sum(case_amount) AS cases
FROM Table_1 t1
WHERE department_nr = (
    SELECT department_nr 
    FROM Table_1 t2
    WHERE t2.hospital_nr = t1.hospital_nr
      AND t2.diagnosis_nr IN (1, 3, 4)
    ORDER BY sum(case_amount) DESC
    LIMIT 1
)
GROUP BY hospital_nr, department_nr

注意:如果两个部门具有相同的总和,则查询将“仅”选择一个。如果要在这种情况下定义要选择哪一个,则应在ORDER BY子句中添加一列(例如department_id)。