我在 Postgres 中有一个表
orders
,其中有列 offer_id
和 date
。我需要编写一个查询,使用之前的非空值来填充列 offer_id
中的所有空情况。
我尝试使用窗口函数
lag()
,但它只能管理一个 NULL,而不是多个。如何解决这个问题?
我试过这个:
with orders as (
select 2 as offer_id, '2021-01-01'::date as date union all
select 3 as offer_id, '2021-01-02'::date as date union all
select null as offer_id, '2021-01-03'::date as date union all
select null as offer_id, '2021-01-04'::date as date union all
select null as offer_id, '2021-01-05'::date as date union all
select 4 as offer_id, '2021-01-07'::date as date union all
select 5 as offer_id, '2021-01-08'::date as date union all
select null as offer_id, '2021-01-09'::date as date union all
select 8 as offer_id, '2021-01-10'::date as date union all
select 9 as offer_id, '2021-01-11'::date as date union all
select null as offer_id, '2021-01-12'::date as date union all
select null as offer_id, '2021-01-13'::date as date union all
select 13 as offer_id, '2021-01-14'::date as date union all
select 13 as offer_id, '2021-01-15'::date as date union all
select null as offer_id, '2021-01-16'::date as date
)
SELECT *, CASE WHEN offer_id IS NULL
THEN LAG(offer_id) OVER (ORDER BY date) ELSE offer_id END AS updated_offer_id
FROM orders
可以通过形成组的子查询来解决:
SELECT offer_id, date
, first_value(offer_id) OVER (PARTITION BY grp ORDER BY date) AS updated_offer_id
FROM (
SELECT *, count(offer_id) OVER (ORDER BY date) AS grp
FROM orders
) sub;
由于
count()
只计算非空值,因此所有包含 null 的行都属于具有前一个非空值的一组。SELECT
中,只需为所有人选择第一个组成员的值即可。
由于缺少前导模板值,前导 null 值保持为 null。相关: