Perform table grouping with two parameters

Asked

Viewed 68 times

2

good night. I’m still learning database and would like to ask a question with you.

I know that if I want to organize my table with the invoiced values per month, I can use group by and filter it this way:

SELECT to_char(DATA, 'YYYY-MM') as MES, sum(QTDUSUARIOS) as QTDUSUARIOS, sum(VALORFATURADO) as VALORFATURADO 
from UBER 
group by to_char(DATA, 'YYYY-MM') order by 1;

and get a result like this:

MES           QTDUSUARIOS     VALORFATURADO

2018-01       244             3.000
2018-02       900             2.500
2018-03       320             1.000

But assuming I also need to group by county and month, more or less like this:

MES           QTDUSUARIOS       MUNICIPIO       VALORFATURADO

2018-01       136               SERRA            2.000
2018-01       108               CAMBURI          1.000
2018-02       500               SERRA            1.000
2018-02       400               CAMBURI          1.500
2018-03       200               SERRA            500
2018-03       120               CAMBURI          500

How would you group like that? I tried to add more parameters in my group by, but it always generates an error saying that the command was not terminated properly.

1 answer

2


Without knowing the whole structure of the table I would say it will be something like this:

SELECT      TO_CHAR(DATA, 'YYYY-MM')    AS MES
        ,   SUM(QTDUSUARIOS)            AS QTDUSUARIOS
        ,   MUNICIPIO
        ,   SUM(VALORFATURADO)          AS VALORFATURADO 
FROM        UBER 
GROUP BY    TO_CHAR(DATA, 'YYYY-MM')
        ,   MUNICIPIO
ORDER BY    1

Browser other questions tagged

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