简体   繁体   English

当我按某些列分组时如何计算行数和列数?

[英]How to count rows and sum columns when I group by some columns?

It seems easy, but its not.这似乎很容易,但事实并非如此。 Please read the query:请阅读查询:

    SELECT 
    b.d_comp, b.fk_frnc, 
    CONCAT(
        DATE_FORMAT(b.d_comp, "%d/%m/%Y"), 
        " - ", f.nome
    ) AS alias, 
    FORMAT(SUM(b.vlr), 2, 'de_DE') AS vlr, 
    
    # ACRÉSCIMO DE SOMAS, CASO TOTAL POR LOTE ESTEJA HABILITADO #
    CONCAT(
        '<span class="fw-bldr">Fêmeas</span>:<br>Quantidade | Peso<br>Adultas:',
        # QTD #
        (
            SELECT COUNT(*) 
            FROM bois AS b2
            WHERE 
                b2.genero = 'F' AND
                b2.d_comp = b.d_comp AND
                b2.fk_frnc = b.fk_frnc AND
                b2.novilho IS FALSE
        ),
        ' | ',
        # PESO #
        (
            SELECT SUM(b2.peso) 
            FROM bois AS b2
            WHERE 
                b2.genero = 'F' AND
                b2.d_comp = b.d_comp AND
                b2.fk_frnc = b.fk_frnc AND
                b2.novilho IS FALSE
        ),
        '<br>Novilhas: ',
        (
            SELECT COUNT(*) FROM bois AS b2
            WHERE b2.genero = 'F' AND b2.d_comp = b.d_comp AND
                b2.fk_frnc = b.fk_frnc AND b2.novilho IS TRUE
        ),
        ' | ',
        (
            SELECT SUM(b2.peso) FROM bois AS b2
            WHERE b2.genero = 'F' AND b2.d_comp = b.d_comp AND
                b2.fk_frnc = b.fk_frnc AND b2.novilho IS TRUE
        ),
        '<br>Total: ',
        (   
            SELECT COUNT(*) FROM bois AS b2
            WHERE b2.genero = 'F' AND b2.d_comp = b.d_comp AND b2.fk_frnc = b.fk_frnc
        ),
        ' | ',
        (
            SELECT SUM(b2.peso) FROM bois AS b2
            WHERE b2.genero = 'F' AND b2.d_comp = b.d_comp AND b2.fk_frnc = b.fk_frnc
        )
    ) AS total_lote
FROM 
    bois AS b INNER JOIN 
    fornecedores AS f 
WHERE 
    b.fk_frnc = f.id
GROUP BY b.d_comp, b.fk_frnc

My problem is collect the COUNT of rows and SUM of weight (peso).我的问题是收集行数和重量总和(比索)。 Return of query show the alias total_lote with NULL for any row.查询返回显示别名 total_lote 与 NULL 的任何行。

I edited the code.我编辑了代码。 Sorry.对不起。 This is the correct query.这是正确的查询。 It returns null to alias total_lote .它将 null 返回给别名total_lote

Thanks for all.谢谢大家。

Instead of the six separate sub-queries you can use one with conditional aggregation and put it in as a derived table -代替六个单独的子查询,您可以使用一个带有条件聚合的查询并将其作为派生表放入 -

SELECT
    d_comp,
    fk_frnc,
    SUM(IF(novilho IS FALSE, 1, 0)) AS col1,
    SUM(IF(novilho IS FALSE, peso, 0)) AS col2,
    SUM(IF(novilho IS TRUE, 1, 0)) AS col3,
    SUM(IF(novilho IS TRUE, peso, 0)) AS col4,
    COUNT(*) AS col5,
    SUM(peso) AS col6
FROM bois
WHERE genero = 'F'
GROUP BY d_comp, fk_frnc

So the full query would then be -所以完整的查询将是 -

SELECT 
    b.d_comp,
    b.fk_frnc,
    CONCAT( DATE_FORMAT(b.d_comp, "%d/%m/%Y"), " - ", f.nome) AS alias,
    FORMAT(SUM(b.vlr), 2, 'de_DE') AS vlr,
    
    /* ACRÉSCIMO DE SOMAS, CASO TOTAL POR LOTE ESTEJA HABILITADO */
    CONCAT(
        '<span class="fw-bldr">Fêmeas</span>:<br>Quantidade | Peso<br>Adultas:', /* QTD */ b2.col1, ' | ', /* PESO */ b2.col2,
        '<br>Novilhas: ', b2.col3, ' | ', b2.col4,
        '<br>Total: ', b2.col5, ' | ', b2.col6
    ) AS total_lote
FROM bois AS b
INNER JOIN (

    SELECT
        d_comp,
        fk_frnc,
        SUM(IF(novilho IS FALSE, 1, 0)) AS col1,
        SUM(IF(novilho IS FALSE, peso, 0)) AS col2,
        SUM(IF(novilho IS TRUE, 1, 0)) AS col3,
        SUM(IF(novilho IS TRUE, peso, 0)) AS col4,
        COUNT(*) AS col5,
        SUM(peso) AS col6
    FROM bois
    WHERE genero = 'F'
    GROUP BY d_comp, fk_frnc

) b2 ON b2.d_comp = b.d_comp AND b2.fk_frnc = b.fk_frnc
INNER JOIN fornecedores AS f
    ON b.fk_frnc = f.id    
GROUP BY b.d_comp, b.fk_frnc

I cannot test this or provide a fiddle as you have not included any sample data or an expected output.我无法对此进行测试或提供小提琴,因为您没有包含任何示例数据或预期的 output。 I suspect the inner and outer queries can be combined but cannot be sure as you have not included any supporting information, particularly the relationship between fornecedores AND bois , in your question.我怀疑内部和外部查询可以组合但不能确定,因为您没有在您的问题中包含任何支持信息,特别是fornecedoresbois之间的关系。

PS You really should do away with the concatenation and do this in your client application. PS你真的应该取消连接并在你的客户端应用程序中执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM