Interpolation
Yet another from [Haki](From https://hakibenita.com/sql-for-data-analysis).
Back and Forward Fill
Filling values with constants is easy, but not always possible. Another common interpolation technique is filling empty values with previous or following non-missing values.
Pandas offers several variations on back and forward filling, for example:
| |
To achieve the same using SQL, you can use a subquery:
| |
The SQL version is a bit longer, but it is fairly expressive, and it gives great flexibility.
NOTE: It’s tempting to use the window function LEAD and LAG here, but these function can only be used when filling single row gaps. Once you have more than one consecutive missing row, LEAD and LAG may leave you with missing values.