Advertisement
camilossantos

filter

Apr 24th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. create table historicos (
  2.     id serial primary key,
  3.     problemas float,
  4.     total float,
  5.     data timestamp
  6. );
  7.  
  8. insert into historicos (problemas, total, data)
  9.   values (25, 125, '2017-01-01 00:00:00');
  10. insert into historicos (problemas, total, data)
  11.   values (25, 125, '2017-01-10 00:00:00');
  12. insert into historicos (problemas, total, data)
  13.   values (25, 125, '2017-02-01 00:00:00');
  14. insert into historicos (problemas, total, data)
  15.   values (25, 125, '2017-03-01 00:00:00');
  16. insert into historicos (problemas, total, data)
  17.   values (25, 125, '2017-04-01 00:00:00');
  18. insert into historicos (problemas, total, data)
  19.   values (25, 125, '2017-06-01 00:00:00');
  20. insert into historicos (problemas, total, data)
  21.   values (25, 125, '2017-08-01 00:00:00');
  22. insert into historicos (problemas, total, data)
  23.   values (25, 125, '2017-08-21 00:00:00');
  24. insert into historicos (problemas, total, data)
  25.   values (25, 125, '2017-09-01 00:00:00');
  26. insert into historicos (problemas, total, data)
  27.   values (25, 125, '2017-10-01 00:00:00');
  28. insert into historicos (problemas, total, data)
  29.   values (25, 125, '2017-11-01 00:00:00');
  30. insert into historicos (problemas, total, data)
  31.   values (25, 125, '2017-12-01 00:00:00');
  32.  
  33. select
  34.   sum(problemas) filter (where data between '2017-01-01' and '2017-02-28') as problemas_mes_1,
  35.   sum(total) filter (where data between '2017-01-01' and '2017-02-28') as total_mes_1,
  36.   sum(problemas) filter (where data between '2017-03-01' and '2017-04-30') as problemas_mes_2,
  37.   sum(total) filter (where data between '2017-03-01' and '2017-04-30') as total_mes_2
  38. from historicos;
  39. problemas_mes_1 | total_mes_1 | problemas_mes_2 | total_mes_2
  40. -----------------+-------------+-----------------+-------------
  41.               75 |         375 |              50 |         250
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement