需要帮助MYSQL QUERY(SUM)

时间:2014-07-07 11:02:51

标签: mysql select

我需要一些SELECT QUERY的帮助。我只是想正确总结下表。该表是其他几个表的综合。我可能会为学生的名字而不是文本添加一个id int值,以便更容易理解 - 但这不是我的问题。

我想为每个学生总结三种缺课。但是我的sql每次只有一个!我究竟做错了什么?我尝试了几次更改,但我没有找到它..

我想从我的mysql表中添加一个屏幕,但我没有足够的声誉..

SELECT DISTINCT ( P.synthese_abs_schueler_name ) AS synthese_abs_schueler_name, 
                SUM(A.synthese_sum)              AS absent, 
                SUM(B.synthese_sum)              AS nonexcused, 
                SUM(C.synthese_sum)              AS tardive, 
                SUM(absent + nonexcused)         AS totalabs, 
FROM   synthese_abs AS P 
       LEFT JOIN (SELECT synthese_abs_schueler_name, 
                         synthese_abs_type, 
                         synthese_abs_sum AS synthese_sum 
                  FROM   synthese_abs 
                  WHERE  synthese_abs_type = 1 
                         AND synthese_abs_sum > 0 
                  GROUP  BY synthese_abs_schueler_name) AS A 
              ON A.synthese_abs_type = P.synthese_abs_type 
                 AND A.synthese_abs_schueler_name = P.synthese_abs_schueler_name 
       LEFT JOIN (SELECT synthese_abs_schueler_name, 
                         synthese_abs_type, 
                         synthese_abs_sum AS synthese_sum 
                  FROM   synthese_abs 
                  WHERE  synthese_abs_type = 2 
                         AND synthese_abs_sum > 0 
                  GROUP  BY synthese_abs_schueler_name) AS B 
              ON B.synthese_abs_type = P.synthese_abs_type 
                 AND B.synthese_abs_schueler_name = P.synthese_abs_schueler_name 
       LEFT JOIN (SELECT synthese_abs_schueler_name, 
                         synthese_abs_type, 
                         synthese_abs_sum AS synthese_sum 
                  FROM   synthese_abs 
                  WHERE  synthese_abs_type = 3 
                         AND synthese_abs_sum > 0 
                  GROUP  BY synthese_abs_schueler_name) AS C 
              ON C.synthese_abs_type = P.synthese_abs_type 
                 AND C.synthese_abs_schueler_name = P.synthese_abs_schueler_name 
ORDER  BY P.synthese_abs_schueler_name 

3 个答案:

答案 0 :(得分:1)

您可以按以下方式重写查询,而不是使用多个自我加入,您可以使用CASE中的SUM()根据synthese_sum的值对synthese_abs_type求和缺席和不使用的总和我使用了一个子选择,因为在同一级别的查询中你可以使用聚合结果的别名选择使用子选择或重复整个表达式

SELECT *, absent + nonexcused  AS totalabs
FROM (
SELECT  synthese_abs_schueler_name ,
    SUM(CASE WHEN synthese_abs_type = 1 THEN synthese_sum ELSE 0 END) AS absent,
    SUM(CASE WHEN synthese_abs_type = 2 THEN synthese_sum ELSE 0 END) AS nonexcused,
    SUM(CASE WHEN synthese_abs_type = 3 THEN synthese_sum ELSE 0 END)  AS tardive
FROM 
synthese_abs 
WHERE synthese_abs_sum >0 
GROUP BY synthese_abs_schueler_name 
ORDER BY synthese_abs_schueler_name 
) t

答案 1 :(得分:0)

如何删除您拥有的三个子查询中的GROUP BY子句并将其添加到主查询中:

SELECT P.synthese_abs_schueler_name, 
                SUM(A.synthese_sum)              AS absent, 
                SUM(B.synthese_sum)              AS nonexcused, 
                SUM(C.synthese_sum)              AS tardive, 
                SUM(absent + nonexcused)         AS totalabs, 
FROM   synthese_abs AS P 
       LEFT JOIN (SELECT synthese_abs_schueler_name, 
                         synthese_abs_type, 
                         synthese_abs_sum AS synthese_sum 
                  FROM   synthese_abs 
                  WHERE  synthese_abs_type = 1 
                         AND synthese_abs_sum > 0) AS A 
              ON A.synthese_abs_schueler_name = P.synthese_abs_schueler_name 
       LEFT JOIN (SELECT synthese_abs_schueler_name, 
                         synthese_abs_type, 
                         synthese_abs_sum AS synthese_sum 
                  FROM   synthese_abs 
                  WHERE  synthese_abs_type = 2 
                         AND synthese_abs_sum > 0 ) AS B 
              ON B.synthese_abs_schueler_name = P.synthese_abs_schueler_name 
       LEFT JOIN (SELECT synthese_abs_schueler_name, 
                         synthese_abs_type, 
                         synthese_abs_sum AS synthese_sum 
                  FROM   synthese_abs 
                  WHERE  synthese_abs_type = 3 
                         AND synthese_abs_sum > 0) AS C 
              ON C.synthese_abs_schueler_name = P.synthese_abs_schueler_name 
GROUP  BY P.synthese_abs_schueler_name

答案 2 :(得分:0)

试试这个:

SELECT P.synthese_abs_schueler_name AS synthese_abs_schueler_name,
    SUM(A.synthese_abs_sum) AS absent,
    SUM(B.synthese_abs_sum) AS nonexcused,
    SUM(C.synthese_abs_sum) AS tardive,
    SUM(A.synthese_abs_sum)+SUM(B.synthese_abs_sum) AS totalabs
FROM 
synthese_abs AS P

LEFT JOIN synthese_abs A ON A.synthese_abs_type = 1 AND A.synthese_abs_sum >0 AND A.synthese_abs_schueler_name = P.synthese_abs_schueler_name

LEFT JOIN synthese_abs B ON B.synthese_abs_type = 2 AND B.synthese_abs_sum >0 AND B.synthese_abs_schueler_name = P.synthese_abs_schueler_name

LEFT JOIN synthese_abs C ON C.synthese_abs_type = 3 AND C.synthese_abs_sum >0 AND C.synthese_abs_schueler_name = P.synthese_abs_schueler_name

GROUP BY P.synthese_abs_schueler_name

基本上,对于Schüler的名称来说,按照Schüler获得总和非常重要。