Fazer Count da Mesma Tabela com Filtros Diferentes
03/09/2020
0
Estou quebrando a cabeça aqui e não saio do lugar, alguém consegue me ajudar?
Eu tenho duas tabelas assim:
Tabela1
orderID | date | product | origin | destiny | value
Tabela2
paymentID | process_date | status | ...
E preciso agrupar por origem quantas linhas eu tenho, e depois agrupar por destino quantas linhas eu tenho.
A tabela de saída seria:
month | origin | count_origin | destiny | count_destiny
Quando tentei fazer ele não agrupou, mas mostrava o count por rota (origin-destiny).
Mid
Posts
03/09/2020
Emerson Nascimento
--------+--------------+---------+--------+---------+------------ orderID | date | product | origin | destiny | value --------+--------------+---------+--------+---------+------------ 1 | 2020-01-01 | xxxxxx | SP | SP | nnnn 2 | 2020-01-01 | xxxxxx | SP | RJ | nnnn 3 | 2020-01-01 | xxxxxx | SP | BA | nnnn 4 | 2020-01-01 | xxxxxx | RJ | RJ | nnnn 5 | 2020-01-01 | xxxxxx | RJ | MG | nnnn 6 | 2020-01-01 | xxxxxx | RJ | SP | nnnn 7 | 2020-01-01 | xxxxxx | BA | BA | nnnn 8 | 2020-01-01 | xxxxxx | BA | RJ | nnnn 9 | 2020-01-01 | xxxxxx | BA | PE | nnnn
mostre qual deveria ser o resultado
--------+--------+--------------+---------+---------------- month | origin | count_origin | destiny | count_destiny --------+--------+--------------+---------+---------------- 2001.01 | | | |
03/09/2020
Mid
--------+--------------+---------+--------+---------+------------ orderID | date | product | origin | destiny | value --------+--------------+---------+--------+---------+------------ 1 | 2020-01-01 | xxxxxx | SP | SP | nnnn 2 | 2020-01-01 | xxxxxx | SP | RJ | nnnn 3 | 2020-01-01 | xxxxxx | SP | BA | nnnn 4 | 2020-01-01 | xxxxxx | RJ | RJ | nnnn 5 | 2020-01-01 | xxxxxx | RJ | MG | nnnn 6 | 2020-01-01 | xxxxxx | RJ | SP | nnnn 7 | 2020-01-01 | xxxxxx | BA | BA | nnnn 8 | 2020-01-01 | xxxxxx | BA | RJ | nnnn 9 | 2020-01-01 | xxxxxx | BA | PE | nnnn
mostre qual deveria ser o resultado
--------+--------+--------------+---------+---------------- month | origin | count_origin | destiny | count_destiny --------+--------+--------------+---------+---------------- 2001.01 | | | |
Agradeço pela resposta
A saida esperada seria:
--------+--------+--------------+---------+---------------- month | origin | count_origin | count_destiny --------+--------+--------------+---------+---------------- 2001.01 | SP | 3 | 1 | 2001.01 | RJ | 3 | 3 | 2001.01 | BA | 3 | 2 | 2001.01 | MG | 0 | 1 | 2001.01 | PE | 0 | 1 |
03/09/2020
Mid
--------+--------------+---------+--------+---------+------------ orderID | date | product | origin | destiny | value --------+--------------+---------+--------+---------+------------ 1 | 2020-01-01 | xxxxxx | SP | SP | nnnn 2 | 2020-01-01 | xxxxxx | SP | RJ | nnnn 3 | 2020-01-01 | xxxxxx | SP | BA | nnnn 4 | 2020-01-01 | xxxxxx | RJ | RJ | nnnn 5 | 2020-01-01 | xxxxxx | RJ | MG | nnnn 6 | 2020-01-01 | xxxxxx | RJ | SP | nnnn 7 | 2020-01-01 | xxxxxx | BA | BA | nnnn 8 | 2020-01-01 | xxxxxx | BA | RJ | nnnn 9 | 2020-01-01 | xxxxxx | BA | PE | nnnn
mostre qual deveria ser o resultado
--------+--------+--------------+---------+---------------- month | origin | count_origin | destiny | count_destiny --------+--------+--------------+---------+---------------- 2001.01 | | | |
Agradeço pela resposta
A saida esperada seria:
--------+--------+--------------+---------+---------------- month | origin | count_origin | count_destiny --------+--------+--------------+---------+---------------- 2001.01 | SP | 3 | 1 | 2001.01 | RJ | 3 | 3 | 2001.01 | BA | 3 | 2 | 2001.01 | MG | 0 | 1 | 2001.01 | PE | 0 | 1 |
Não precisa trazer a cidade duas vezes, agora que percebi. Inclusive acho que da pra trocar o nome da tabela e deixar cidade.
--------+--------+--------------+---------+---------------- month | city | count_origin | count_destiny --------+--------+--------------+---------+---------------- 2001.01 | SP | 3 | 1 | 2001.01 | RJ | 3 | 3 | 2001.01 | BA | 3 | 2 | 2001.01 | MG | 0 | 1 | 2001.01 | PE | 0 | 1 |
03/09/2020
Emerson Nascimento
SELECT YEARMONTH, CITY, SUM(C_ORIGEM) COUNT_ORIGIN, SUM(C_DESTINY) COUNT_DESTINY FROM ( SELECT YEAR(DATE)+(MONTH(DATE)/100.0) YEARMONTH, ORIGIN CITY, COUNT(*) C_ORIGIN, 0 C_DESTINY FROM TABELA1 GROUP BY YEAR(DATE)+(MONTH(DATE)/100.0), ORIGIN UNION ALL SELECT YEAR(DATE)+(MONTH(DATE)/100.0), DESTINY, 0 C_ORIGIN, COUNT(*) C_DESTINY FROM TABELA1 GROUP BY YEAR(DATE)+(MONTH(DATE)/100.0), DESTINY ) T GROUP BY YEARMONTH, CITY