Skip to content
Advertisement

Query the Column based on the value of another column

Hi I have a table structure like this

Here id-rank is the concatenation of id and rank. Now if the rank is greater than 1 (say 2) then I want to extract value column data for id-rank 1-1 i.e somevalue1.

Here is the sql that I have written

The expected output is:

My current output is

But what I get is null in new_value column. I guess this is because it is comparing the adjacent columns only. I am new to sql please let me know if anyone has a solution for this.

Thanks in advance

Advertisement

Answer

You can use lag() :

EDIT : If you are looking for specific previous value then only lag() will be sufficient.

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