Carlos Arruda
Carlos Arruda

Reputation: 15

Sum with aggregate date postgresql

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

Upvotes: 1

Views: 1897

Answers (1)

Erwin Brandstetter
Erwin Brandstetter

Reputation: 656391

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:

Upvotes: 2

Related Questions