This is my sample data
id|score|
--+-----+
1| 10|
2| 20|
3| 30|
4| 40|
5| 50|
6| 60|
7| 30|
8| 30|
9| 90|
10| 100|
My expected output is
id|score|_prev_element|
--+-----+-------------+
1| 10| NULL|
2| 20| 10|
3| 30| 20|
4| 40| 20|
5| 50| 40|
6| 60| 50|
7| 30| 60|
8| 30| 60|
9| 90| 60|
10| 100| 90|
_prev_element – want to display Previous element ie for id=2 then value 10 for previous element. if Previous element is 30 then skip and print recent value which is not 30.
for example for id =8 previous element is 30 so i skip and pick 60
for id=9 previous element 8 and 7 ‘s are 30 so skip and pick 60. How can i achieve this result. I tried leg(score) over(order by id)..No luck
2
Answers
You can do:
Result:
I’m sure there’s a cleaner way of doing this but I can’t think of any off the top of my head.
See running example at db<>fiddle.
Try this option without window functions.
fiddle