Forward (or Backward filling) in postgres
<p>The problem is to fill missing values in a table. In <a href="http://pandas.pydata.org" rel="noreferrer">pandas</a>, one can use forward (or backward) filling to do so as shown below:</p>

<pre><code>$&gt; import pandas as pd
$&gt; df = pd.DataFrame({'x': [None, 1, None, None, 2, None, 3, None]})
$&gt; df['y'] = df['x'].fillna(method='ffill')
$&gt; 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
</code></pre>

<p>Is there a way to do that in SQL and more precisely in PostGres? I guess <a href="https://www.postgresql.org/docs/9.5/static/functions-window.html" rel="noreferrer">window functions</a> could help but i couldn't figure out how.</p>

<p>In PostGres, it would be like:</p>

<pre><code>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)
</code></pre>