I have a table in Postgres with categories and values. I want to perform an aggregation, e.g. avg(value) per category but including the first row of the next category in the aggregation.
Sample table:
id category value
-------------------
1 1 5.4
2 1 2.1
3 2 1.0
4 2 2.6
5 2 0.3
6 3 4.4
7 3 3.8
id
is a primary key and provides an order. Categories are grouped within the order and consecutive.
It would be acceptable (but not necessary) to create an intermediate table like this, which is duplicating the adjacent row:
id category value
-------------------
1 1 5.4
2 1 2.1
3 1 1.0 <-- new row
4 2 1.0
5 2 2.6
6 2 0.3
7 2 4.4 <-- new row
8 3 4.4
9 3 3.8
... and then do:
select category, avg(value) group by category from sample_table
How can this be achieved with SQL statements?
I suspect this could be done with window functions and some sophisticated frame clause like GROUPS, but I don't know how. ( See https://www.postgresql.org/docs/12/sql-expressions.html#SYNTAX-WINDOW-FUNCTIONS )