Skip to content
Advertisement

SQL lag function avoid getting first row as null

I have a sql query

if my filter date is > 20210625 , I get value as 27 and previous value as null , is it possible to get the prev_value 25. If there is no filter I will get it. But filter is required to prevent reading the whole table. Also one more requirement. If previous value is null for example , date – 20210628, I should be getting previous non-null value that is 27.

Advertisement

Answer

An option would be to calculate the last known value from the table and use it as a default value in LAG:

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement