MySQL SUM(a,b)使用SQL

时间:2015-06-24 09:00:41

标签: mysql sql

我有一个代码片段如下。我想做SELECT SUM(COALESCE(a1.OGW,0),COALESCE(a2.AGW,0)) AS OW & remove COALESCE(a2.AGW,0) AS AW

我在Google上尝试了一些使用SUM(a) + SUM(b)种类的建议,但似乎没有用。任何建议都会让我非常满意。

 SELECT COALESCE(a1.OGW,0) AS OW,
            COALESCE(a2.AGW,0) AS AW,
            COALESCE(a3.ORGW,0) AS ORW,
            COALESCE(a4.ORS_UI_GC_WT,0) AS ORWU
    FROM
    (
        SELECT COALESCE(SUM(t1.GWO),0)  AS OGW 
        FROM (
                SELECT F_DATE, 
                    CASE
                        WHEN COUNT(F_IN_NAME) = 1 THEN   1
                        WHEN COUNT(F_IN_NAME) = 2 THEN   2
                        WHEN COUNT(F_IN_NAME) BETWEEN 3 AND 5 THEN  3
                        WHEN COUNT(F_IN_NAME)  > 5 THEN  5
                        ELSE 0
                    END AS GWO
                FROM
                    FWH t1
                WHERE 
                    F_IN_NAME IN (
                        SELECT I_NAME from I_W_D WHERE IN_ID IN (
                            SELECT I_MAP_ID FROM T_I_MAP_ID where T_MAP_ID = (
                                SELECT T_ID FROM T_W_D WHERE T_NAME = 'paia'
                            )
                        )
                    ) 
                AND F_DATE between '2015-01-01' and '2015-08-01' 
                AND F_IN_NAME NOT LIKE 'x%rt%' 
                AND F_IN_NAME like 'pv%fg%' 
            GROUP BY F_DATE
         ) t1
     ) a1
     ,
    (
        SELECT COALESCE(SUM(t2.GC_WT_AOD),0) AS AGW 
        FROM (
                SELECT F_DATE,
                    CASE
                        WHEN COUNT(F_IN_NAME)  > 10 THEN  1
                        ELSE 0
                    END AS GC_WT_AOD
                FROM
                    FWH
                WHERE 
                    F_IN_NAME IN ( 
                        SELECT I_NAME from I_W_D WHERE IN_ID IN (
                            SELECT I_MAP_ID FROM T_I_MAP_ID where T_MAP_ID = (
                                SELECT T_ID FROM T_W_D WHERE T_NAME = 'paia'
                            )
                        )
                    ) 
                AND F_DATE between '2015-01-01' and '2015-08-01' 
                AND F_IN_NAME NOT LIKE 'x%rt%' 
                AND F_IN_NAME like 'x%rs%%' 
            GROUP BY F_DATE
        ) t2
    ) a2
     ,
    (
        SELECT COALESCE(SUM(t3.GC_WT_ORS),0) AS ORGW 
        FROM (
                SELECT F_DATE,
                    CASE
                        WHEN COUNT(F_IN_NAME)  > 10 THEN  1
                        ELSE 0
                    END AS GC_WT_ORS
                FROM
                    FWH
                WHERE F_IN_NAME IN ( 
                    SELECT I_NAME from I_W_D WHERE IN_ID IN (
                        SELECT I_MAP_ID FROM T_I_MAP_ID where T_MAP_ID = (
                            SELECT T_ID FROM T_W_D WHERE T_NAME = 'paia'
                        )
                    )
                ) 
                AND F_DATE between '2015-01-01' 
                AND '2015-08-01' AND F_IN_NAME NOT LIKE 'x%rt%'
                AND F_IN_NAME like 'wd%ors%' 
                AND F_IN_NAME NOT LIKE 'y%rt%' 
            GROUP BY F_DATE
        ) t3
    ) a3
     ,
    (
        SELECT COALESCE(SUM(t4.GC_WT_ORS_UI),0) AS ORS_UI_GC_WT 
        FROM (
                SELECT F_DATE,
                CASE
                    WHEN COUNT(F_IN_NAME) = 1 THEN   1
                    WHEN COUNT(F_IN_NAME) BETWEEN 2 AND 5 THEN  2
                    WHEN COUNT(F_IN_NAME)  > 5 THEN  3
                    ELSE 0
                    END AS GC_WT_ORS_UI
                FROM
                    FWH
                WHERE F_IN_NAME IN ( 
                    SELECT I_NAME from I_W_D WHERE IN_ID IN (
                        SELECT I_MAP_ID FROM T_I_MAP_ID where T_MAP_ID = (
                            SELECT T_ID FROM T_W_D WHERE T_NAME = 'paia'
                        )
                    )
                ) 
                AND F_DATE between '2015-01-01' and '2015-08-01' 
                AND F_IN_NAME NOT LIKE 'x%rt%' 
                AND F_IN_NAME like 'y%pt%' 
            GROUP BY F_DATE
        ) t4
    ) a4

更新 - 2 =>格式化更好

1 个答案:

答案 0 :(得分:1)

此表达式无效SQL,因为SUM()只接受一个参数:

SELECT SUM(COALESCE(a1.OGW,0),COALESCE(a2.AGW,0))  AS OW 

我猜你打算:

SELECT SUM(COALESCE(a1.OGW, 0) + COALESCE(a2.AGW, 0))  AS OW