Skip to content
Advertisement

traverse to previous rows from current row in a table

I need to traverse to previous 10 rows from current row and check if all rows has codealert_hour . If all rows has alerts then display prev row alert else display null.

image shows sample data.

current row is id=1176.

Now it has to traverse from id 1175 to 1166 and check all alerts. Here all alerts are present (i.e no null alerts) so it should fetch prev row alert (i.e id 1175 alert= 5) in a column of current row.

I tried using lag function but its not working. please help.

enter image description here

Advertisement

Answer

You can do it using LAG and also using self join.

Here is the example using LAG.

Here is the example using self join

Cheers!!

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