postgres 中的向前(或向后填充)

Gui*_*mas 5 sql postgresql window-functions pandas

问题是在表中填充缺失值。在pandas 中,可以使用向前(或向后)填充来执行此操作,如下所示:

$> import pandas as pd
$> df = pd.DataFrame({'x': [None, 1, None, None, 2, None, 3, None]})
$> df['y'] = df['x'].fillna(method='ffill')
$> df
    x   y
0 NaN NaN
1   1   1
2 NaN   1
3 NaN   1
4   2   2
5 NaN   2
6   3   3
7 NaN   3
Run Code Online (Sandbox Code Playgroud)

有没有办法在 SQL 中更准确地在 PostGres 中做到这一点?我想窗口函数可以提供帮助,但我不知道如何。

在 PostGres 中,它会是这样的:

sandbox=# SELECT x, ??
FROM
  (SELECT NULL AS x
   UNION ALL SELECT 1 AS x
   UNION ALL SELECT NULL AS x
   UNION ALL SELECT NULL AS x
   UNION ALL SELECT 2 AS x
   UNION ALL SELECT NULL AS x
   UNION ALL SELECT 3 AS x
   UNION ALL SELECT NULL AS x) a;
 x 
---

 1


 2

 3

(8 rows)
Run Code Online (Sandbox Code Playgroud)

Vao*_*sun 7

窗口函数在这里

这么多别名,因为您的查询对订单非常敏感。我添加了更多的空x行来证明它对几个空行进行了修剪......

select x,y from (
select r,x, case when y is not null then y else min(y) over (partition by x order by r) end y from (
SELECT row_number() over() r,x, case when x is not null then x else lag(x) over () end y
FROM
  (SELECT NULL AS x
   UNION ALL SELECT 1 AS x
   UNION ALL SELECT NULL AS x
   UNION ALL SELECT NULL AS x
   UNION ALL SELECT NULL AS x
   UNION ALL SELECT NULL AS x
   UNION ALL SELECT 2 AS x
   UNION ALL SELECT NULL AS x
   UNION ALL SELECT 3 AS x
   UNION ALL SELECT NULL AS x
   ) a
   ) b
order by r
   ) c
   ;
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明