First and last value of window function in one row in PostgreSQL
The question is old, but this solution is simpler and faster than what's been posted so far:
SELECT b.machine_id
, batch
, timestamp_sta
, timestamp_stp
, min(timestamp_sta) OVER w AS batch_start
, max(timestamp_stp) OVER w AS batch_end
FROM db_data.sta_stp a
JOIN db_data.ll_lu b ON a.ll_lu_id = b.id
WINDOW w AS (PARTITION BY batch, b.machine_id) -- No ORDER BY !
ORDER BY timestamp_sta, batch, machine_id; -- why this ORDER BY?
If you add ORDER BY
to the window frame definition, each next row with a greater ORDER BY
expression has a later frame start. Neither min()
nor first_value()
can return the "first" timestamp for the whole partition then. Without ORDER BY
all rows of the same partition are peers and you get your desired result.
Your added ORDER BY
works (not the one in the window frame definition, the outer one), but doesn't seem to make sense and makes the query more expensive. You should probably use an ORDER BY
clause that agrees with your window frame definition to avoid additional sort cost:
...
ORDER BY batch, b.machine_id, timestamp_sta, timestamp_stp;
I don't see the need for DISTINCT
in this query. You could just add it if you actually need it. Or DISTINCT ON ()
. But then the ORDER BY
clause becomes even more relevant. See:
- Select first row in each GROUP BY group?
If you need some other column(s) from the same row (while still sorting by timestamps), your idea with FIRST_VALUE()
and LAST_VALUE()
might be the way to go. You'd probably need to append this to the window frame definition then:
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
See:
- PostgreSQL query with max and min date plus associated id per row
The explanations given by @Łukasz Kamiński solve the core of the issue.
However, the last_value
should be replaced by max()
. You are sorting by timestamp_sta
so the last value is the one having the greatest timestamp_sta
, which may or may not be related to timestamp_stp
. Also I would sort by the two fields.
SELECT DISTINCT
b.machine_id,
batch,
timestamp_sta,
timestamp_stp,
FIRST_VALUE(timestamp_sta) OVER w AS batch_start,
MAX(timestamp_stp) OVER w AS batch_end
FROM db_data.sta_stp AS a
JOIN db_data.ll_lu AS b
ON a.ll_lu_id=b.id
WINDOW w AS (PARTITION BY batch, machine_id
ORDER BY timestamp_sta,timestamp_stp
RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)
ORDER BY timestamp_sta, batch, machine_id;
http://rextester.com/UTDE60342
From syntax documentation:
The frame_clause specifies the set of rows constituting the window frame, which is a subset of the current partition, for those window functions that act on the frame instead of the whole partition. The frame can be specified in either RANGE or ROWS mode; in either case, it runs from the frame_start to the frame_end. If frame_end is omitted, it defaults to CURRENT ROW.
A frame_start of UNBOUNDED PRECEDING means that the frame starts with the first row of the partition, and similarly a frame_end of UNBOUNDED FOLLOWING means that the frame ends with the last row of the partition.
and function list
last_value(value any) returns value evaluated at the row that is the last row of the window frame
So correct SQL should be:
SELECT DISTINCT
b.machine_id,
batch,
timestamp_sta,
timestamp_stp,
FIRST_VALUE(timestamp_sta) OVER w AS batch_start,
LAST_VALUE(timestamp_stp) OVER w AS batch_end
FROM db_data.sta_stp AS a
JOIN db_data.ll_lu AS b
ON a.ll_lu_id=b.id
WINDOW w AS (PARTITION BY batch, machine_id ORDER BY timestamp_sta range between unbounded preceding and unbounded following)
ORDER BY timestamp_sta, batch, machine_id;