Assume you have (in Postgres 9.1 ) a table like this:
date | value
which have some gaps in it (I mean: not every possible date between min(date) and max(date) has it's row).
My problem is how to aggregate this data so that each consistent group (without gaps) is treated separately, like this:
min_date | max_date | [some aggregate of "value" column]
Any ideas how to do it? I believe it is possible with window functions but after a while trying with lag()
and lead()
I'm a little stuck.
For instance if the data are like this:
date | value
---------------+-------
2011-10-31 | 2
2011-11-01 | 8
2011-11-02 | 10
2012-09-13 | 1
2012-09-14 | 4
2012-09-15 | 5
2012-09-16 | 20
2012-10-30 | 10
the output (for sum
as the aggregate) would be:
min | max | sum
-----------+------------+-------
2011-10-31 | 2011-11-02 | 20
2012-09-13 | 2012-09-16 | 30
2012-10-30 | 2012-10-30 | 10
Here is a way of solving it.
First, to get the beginning of consecutive series, this query would give you the first date:
likewise for the end of consecutive series,
You might consider making these views, to simplify queries using them.
We only need the first to form group ranges
Simpler and cheaper version:
My first try was more complex and expensive:
The output: