I have a table with columns:
id (PK),
low_value,
high_value
Id and high_value is populated in this table and low_value needs to be populated using populated data. Low_value should be the previous ordered high_value in the table.
Example - table data before updating low_value:
id | low_value | high_value
---------------------------------------
1 | | 1000
2 | | 1000
3 | | 2000
4 | | 5000
5 | | 7000
6 | | 10000
7 | | 10000
8 | | 20000
table data after updating low_value should be:
id | low_value | high_value
---------------------------------------
1 | 0 | 1000
2 | 0 | 1000
3 | 1000 | 2000
4 | 2000 | 5000
5 | 5000 | 7000
6 | 7000 | 10000
7 | 7000 | 10000
8 | 10000 | 20000
I am thinking of assigning a rank (using window function rank()) to each row based upon high_value, so rows with same high_value will have same rank and then pick the value with rank less than current rank for the row getting updated. But i am not able to construct the sql. Can anyone help with the sql with or without ranking rows ?