我有2个查询,只有一个要合并在一个查询中的差异

时间:2018-12-12 12:09:35

标签: mysql join group-by

我在MySQL中有两个基本相同的查询,它们之间的唯一区别是 min() max() ,我想知道将两者合并是否可行。

我相信,两者的结合将使我再次脱离工作,减少返工。

我的目标是从第一天和最后一天开始了解“ valor_bruto_atual”。

第一个查询:

SELECT 
    titulo_id,
    SUM(valor_bruto_atual) AS valor_max,
    data_imports.data_import AS created_at,
    week(data_imports.data_import) AS weeknumber
FROM
    ativos_extratos
        INNER JOIN
    titulos ON titulo_id = titulos.id
        INNER JOIN
    representantes ON representante_id = representantes.id
        INNER JOIN
    data_imports ON data_import_id = data_imports.id
WHERE
    user_id = 1
        AND ativos_extratos.data_import_id IN (SELECT 
            MAX(ID)
        FROM
            data_imports
        WHERE
            data_import BETWEEN '2018-11-01' AND '2018-12-10'
        GROUP BY week(data_import))
        AND data_imports.data_import BETWEEN '2018-11-01' AND '2018-12-10'
GROUP BY titulos.nome_titulo , weeknumber
ORDER BY data_import ASC , titulos.nome_titulo ASC

第二个查询:

SELECT 
    titulo_id,
    SUM(valor_bruto_atual) AS valor_min,
    data_imports.data_import AS created_at,
    week(data_imports.data_import) AS weeknumber
FROM
    ativos_extratos
        INNER JOIN
    titulos ON titulo_id = titulos.id
        INNER JOIN
    representantes ON representante_id = representantes.id
        INNER JOIN
    data_imports ON data_import_id = data_imports.id
WHERE
    user_id = 1
        AND ativos_extratos.data_import_id IN (SELECT 
            MIN(ID)
        FROM
            data_imports
        WHERE
            data_import BETWEEN '2018-11-01' AND '2018-12-10'
        GROUP BY week(data_import))
        AND data_imports.data_import BETWEEN '2018-11-01' AND '2018-12-10'
GROUP BY titulos.nome_titulo , weeknumber
ORDER BY data_import ASC , titulos.nome_titulo ASC

唯一的更改是该列的精美名称以及where条件中的 min() max() 函数。

1 个答案:

答案 0 :(得分:0)

解决方案:

SELECT 
    titulo_id as titulo_id,
-- calculate both SUMs, select values for each dependent by what value it matches to
    SUM(CASE WHEN ativos_extratos.data_import_id = borders.min_id THEN valor_bruto_atual END) AS valor_init,
    SUM(CASE WHEN ativos_extratos.data_import_id = borders.max_id THEN valor_bruto_atual END) AS valor_end,    
    data_imports.data_import AS created_at,
    month(data_imports.data_import) AS weeknumber
FROM
    ativos_extratos
        INNER JOIN
    titulos ON titulo_id = titulos.id
        INNER JOIN
    representantes ON representante_id = representantes.id
        INNER JOIN
    data_imports ON data_import_id = data_imports.id
-- moved subquery and condition by it from WHERE 
    INNER JOIN ( SELECT 
                    MIN(ID) as min_id,
                    MAX(ID) as max_id
                 FROM
                    data_imports
                 WHERE
                    data_import BETWEEN '2018-10-01' AND '2018-12-10'
                 GROUP BY month(data_import)
               ) borders ON ativos_extratos.data_import_id IN (borders.min_id, borders.max_id)
WHERE   
    user_id = 1
-- removed subquery and condition were there
        AND data_imports.data_import BETWEEN '2018-10-01' AND '2018-12-10'
GROUP BY titulos.nome_titulo , weeknumber
ORDER BY data_import ASC , titulos.nome_titulo ASC

参考:https://dba.stackexchange.com/questions/224804/i-have-2-queries-with-only-one-difference-that-i-want-to-merge-in-just-one-query/