0
Good evening staff. I am a student beginner in database and would like to take a question for study purposes.
Suppose I have the following structure:
MES QTDUSUARIOS MUNICIPIO VALORFATURADO
2018-01 136 SERRA 2000
2018-01 108 CAMBURI 1000
2018-02 500 SERRA 1000
2018-02 400 CAMBURI 1500
2018-03 200 SERRA 500
2018-03 120 CAMBURI 500
And create a new column containing the amount invoiced in the previous month, example:
MES QTDUSUARIOS MUNICIPIO VALORFATURADO VALOR_MES_ANTERIOR
2018-02 500 SERRA 1000 2000
2018-02 400 CAMBURI 1500 1000
2018-03 200 SERRA 500 1000
2018-03 120 CAMBURI 500 1500
How would I do it? Currently, I perform the consultation in this way:
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;
Script of table creation:
CREATE TABLE UBER (
ID NUMBER,
DATA DATE,
QTDUSUARIOS NUMBER(6),
MUNICIPIO VARCHAR2(50),
VALORFATURADO NUMBER(8,2)
)
Example of data entry:
Insert into UBER (ID,DATA,QTDUSUARIOS,MUNICIPIO,VALORFATURADO) values ('1',to_date('10/01/2018 00:00:00','DD/MM/YYYY HH24:MI:SS'),'136','SERRA','2000');
Make a subselect as a column.
– Motta
Good evening Motta. Would you have examples to complement your answer? Links, tips, anything. Thank you!
– Codeman
Good note @Codeman would be very useful if instead of simple examples you put the script for creating this table and inserting the data, so it would be much easier to reproduce what you have and send you a more complete answer
– viniciusxyz
Yes, sorry. I updated the posting description, thanks for the notice!
– Codeman