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

1 Answers1

2

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
  • 605,456
  • 145
  • 1,078
  • 1,228