简体   繁体   中英

How to group one more column and sum the amount of items? [SQL]

orig : Como agrupar mais uma coluna e somar a quantidade dos itens

SELECT 
        u.nome,
        ef.nome AS nome_etapa,
        count(l.id) AS quantidade

        FROM usuario u
            LEFT join lead l ON l.id_usuario = u.id
            LEFT JOIN etapa_funil ef ON ef.id = l.id_etapa

                where
                    u.permissao = "2"
                        GROUP BY l.id, nome_etapa
                        ORDER BY u.nome, ef.posicao ASC;

Data

|nome     |nome_etapa|quantidade <br>
|Ana Paula| Entrada  |1| <br>
|Ana Paula| Perdidos |1| <br>
|Ana Paula| Perdidos |1| <br>
|Ana Paula| Perdidos |1| <br>
|Ana Paula| Perdidos |1| <br>

I need the last to group together and show the amount as 4

orig : Precisava que os perdidos agrupassem e mostrassem a quantidade como 4

Do not group by ID. Group by names instead

não agrupar por id. agrupar por nome(s)

    SELECT 
    u.nome,
    ef.nome AS nome_etapa,
    count(l.id) AS quantidade
    FROM usuario u
        LEFT join lead l ON l.id_usuario = u.id
        LEFT JOIN etapa_funil ef ON ef.id = l.id_etapa
            where
                u.permissao = "2"
                    GROUP BY u.nome, ef.nome  <--GROUP BY nomes
                    ORDER BY u.nome, ef.nome ASC;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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