1

I have a table that has a column date and value, what I need is to sum a value showing just one date column. Ex: I have this:

date       value
2018-01-01 150
2018-01-23 140

what I need:

date    sum(value)
2018-01 290
a_horse_with_no_name
  • 497,550
  • 91
  • 775
  • 843

1 Answers1

1

Simple solution to get sums per month:

SELECT to_char(date, 'YYYY-MM') AS mon, sum(value) AS sum_value
FROM   tbl
GROUP  BY 1;

For large tables it's cheaper to group on date_trunc('month', date) instead.

Related:

Erwin Brandstetter
  • 539,169
  • 125
  • 977
  • 1,137