I have a table like this in PostgreSQL. I want to perform aggregation functions like mean and max for every 16 records based on ID (which is primary key). For example I have to calculate mean value for first 16 records and second 16 records and so on.
+-----+-------------
| ID | rainfall |
+-----+----------- |
| 1 | 110.2 |
| 2 | 56.6 |
| 3 | 65.6 |
| 4 | 75.9 |
+-----+------------
The 1st approach that comes to mind is to use
row_number()to annotate the table, then group by blocks of 16 rows.Note that this won’t necessarily include 16 samples for the last group.
Alternately you can calculate a running average by using
avg()as a window function:… possibly annotating that with the row number and selecting the ones you want:
This will disregard the last n<16 samples, not returning a row for them.
Note that I’m assuming the IDs aren’t guaranteed to be contiguous. If they are gap-less, you can just
group by id/16and avoid the window function.