我在 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 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 o
ORDER BY date
) sub;
由于
count()
只计算非空值,因此所有包含 null 的行都属于具有前一个非空值的一组。SELECT
中,只需为所有人选择第一个组成员的值即可。