Mysql,结合了同一个表的两列

时间:2017-01-30 02:57:56

标签: mysql

我有这样的查询:

SELECT DISTINCT
    m.ucode_outlet AS ucode_grup,
    m.kode_outlet AS kode_grup,
    ROUND(SUM(CASE
                WHEN tot.ucode_jns_brg = '40004' THEN tot.tot_qty
                ELSE 0
            END),
            2) AS 'qty_Jeruk',
    ROUND(IFNULL(SUM(CASE
                        WHEN tot.ucode_jns_brg = '40004' THEN tot.tot_qty
                        ELSE 0
                    END) / SUM(tot.tot_qty) * 100,
                    0),
            2) AS 'psn_Jeruk',
    ROUND(SUM(CASE
                WHEN tot.ucode_jns_brg = '40005' THEN tot.tot_qty
                ELSE 0
            END),
            2) AS 'qty_Jeruk_Multi',
    ROUND(IFNULL(SUM(CASE
                        WHEN tot.ucode_jns_brg = '40005' THEN tot.tot_qty
                        ELSE 0
                    END) / SUM(tot.tot_qty) * 100,
                    0),
            2) AS 'psn_Jeruk_Multi',
    ROUND(IFNULL(SUM(tot.tot_qty), 0), 2) AS total,
    ROUND(IFNULL(SUM(tot.tot_qty) / SUM(tot.tot_qty) * 100,
                    0),
            2) AS total_psn
FROM
    tb_m_outlet m
        LEFT OUTER JOIN
    tp_m_tot_survey tot ON tot.kode_grup = m.kode_outlet
        AND tot.id_proc = 'C9C48EF838B76B415266C553B7FC6ECC'
WHERE
    m.ucode_div LIKE '%%'
        AND m.kode_outlet LIKE '%'
        AND m.nama_outlet LIKE '%'
GROUP BY m.ucode_outlet , m.kode_outlet , m.nama_outlet
ORDER BY m.kode_outlet
LIMIT 0 , 25

和这样的结果: Query result

如果我想将qty_jeruk和qty_jeruk_multi组合成qty_jeruk?

感谢

1 个答案:

答案 0 :(得分:0)

假设通过组合,您将两列的总和合并为一,只需使用IN在一列中添加两者的总和。

    . . .,
    ROUND(SUM(CASE
                WHEN tot.ucode_jns_brg in ('40004','40005') THEN tot.tot_qty
                ELSE 0
            END),
            2) AS 'qty_Jeruk',
    . . .