step-by-step demo:db<>fiddle
SELECT
grp,
gs::date as mydate,
value
FROM (
SELECT
*,
COALESCE( -- 2
lead(mydate) OVER (PARTITION BY grp ORDER BY mydate) - 1, -- 1
mydate
) as prev_date
FROM foo
) s,
generate_series(mydate, prev_date, interval '-1 day') as gs -- 3
ORDER BY grp, mydate DESC -- 4
lead()
window function shifts the next value of an ordered group (= partition) into the current one. The group is already defined, the order is the date
. This can be used to create the required date range. Since you don't want to have the last date twice (as end of the first range and beginning of the next one) the end date stops - 1
(one day before the next group starts)
- This is for the very last records of the groups: They don't have a following record, so
lead()
yield NULL
. To avoid this, COALESCE()
sets them to the current record.
- Now, you can create a date range with the current and the next date value using
generate_series()
.
- Finally you can generate the required order
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…