In Postgres, we want to use the window function as an aggregate function.
We have a table, where every line consists of two timestamps and a value. We first extend the table by adding a column with the difference between timestamps - only a few results are possible. Then we group data by timestamp1 and timediff. In each group, there can be more than one line. We need to choose in each group one value, the one that has the smallest timestamp2.
SELECT
timestamp1,
timediff,
FIRST_VALUE(value) OVER (ORDER BY timestamp2) AS value
FROM (
SELECT
timestamp1,
timestamp2,
value,
timestamp2 - timestamp1 AS timediff
FROM forecast_table WHERE device = 'TEST'
) sq
GROUP BY timestamp1,timediff
ORDER BY timestamp1
Error: column "sq.value" must appear in the GROUP BY clause or be used in an aggregate function