How to join multiple select Count(*) in mysql

Asked

Viewed 206 times

0

I have a table called tb.teste which has 4 columns: status, categoria, mêsand tier.

And in each column, accept the following values:

status (novo, usado, pós venda)
categoria (venda, não venda, aberto)
mês (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
tier (T1, T2, T3, Outro)

I want to return the results of a count of the following query:

mês = '1', tier = 'T1', status = 'novo' e categoria = 'venda'

did so:

select count(*) as contagem_1 from tb_teste
where mes = 1 and status = 'novo' and categoria = 'venda' and tier = 'T1';

So far so good... select me returned the count when all fields meet these conditions...

But I wanted to return in a table of the different results of all possible combinations...

Can someone help me?

1 answer

3

Simply group the four columns and add to the desired function.

SELECT
    status,
    categoria,
    mes,
    tier,
    COUNT(1) as total
FROM tb_teste
GROUP BY status, categoria, mes, tier

This will return all possible combinations between the columns together with the column with the amount of records.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.