I have the following table and from there I want to add another column with the previous value:
id | Previous id | Value |
---|---|---|
30 | 20 | 8000 |
20 | 10 | 5000 |
The output would be as follows:
id | Previous_id | Value | Previous_Value |
---|---|---|---|
30 | 20 | 8000 | 5000 |
20 | 10 | 5000 | - |
I know I could run the self join you can see below, but I want to know if there's a better practice to do so, as this is an operation I will have to run periodically in other similar use cases. I've tried analytic functions but I can't find the solution with them...
SELECT t1.*, t2.Value as Previous_Value
FROM `Table` t1
LEFT JOIN `Table`t2
ON t1.Previous_id = t2.id