Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- create table historicos (
- id serial primary key,
- problemas float,
- total float,
- data timestamp
- );
- insert into historicos (problemas, total, data)
- values (25, 125, '2017-01-01 00:00:00');
- insert into historicos (problemas, total, data)
- values (25, 125, '2017-01-10 00:00:00');
- insert into historicos (problemas, total, data)
- values (25, 125, '2017-02-01 00:00:00');
- insert into historicos (problemas, total, data)
- values (25, 125, '2017-03-01 00:00:00');
- insert into historicos (problemas, total, data)
- values (25, 125, '2017-04-01 00:00:00');
- insert into historicos (problemas, total, data)
- values (25, 125, '2017-06-01 00:00:00');
- insert into historicos (problemas, total, data)
- values (25, 125, '2017-08-01 00:00:00');
- insert into historicos (problemas, total, data)
- values (25, 125, '2017-08-21 00:00:00');
- insert into historicos (problemas, total, data)
- values (25, 125, '2017-09-01 00:00:00');
- insert into historicos (problemas, total, data)
- values (25, 125, '2017-10-01 00:00:00');
- insert into historicos (problemas, total, data)
- values (25, 125, '2017-11-01 00:00:00');
- insert into historicos (problemas, total, data)
- values (25, 125, '2017-12-01 00:00:00');
- select
- sum(problemas) filter (where data between '2017-01-01' and '2017-02-28') as problemas_mes_1,
- sum(total) filter (where data between '2017-01-01' and '2017-02-28') as total_mes_1,
- sum(problemas) filter (where data between '2017-03-01' and '2017-04-30') as problemas_mes_2,
- sum(total) filter (where data between '2017-03-01' and '2017-04-30') as total_mes_2
- from historicos;
- problemas_mes_1 | total_mes_1 | problemas_mes_2 | total_mes_2
- -----------------+-------------+-----------------+-------------
- 75 | 375 | 50 | 250
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement