Here is my table :
sensor_name, ext_value, int_value, growth
47ACXVMACSENS01, 238, 157, 1
47ACXVMACSENS01, 157, 256, 2
47ACXVMACSENS01, 895, 345, 3
47ACXVMACSENS01, 79, 861, 3
91DKCVMACSENS02, 904, 858, 1
91DKCVMACSENS02, 925, 588, 1
91DKCVMACSENS02, 15, 738, 1
91DKCVMACSENS02, 77, 38, 2
The 3 first columns (sensor_name, ext_value, int_value) are given data, the 4th column is a calculated column that I would like to have, this growth column is based on the value of columns (ext_value, int_value) for each group of sensor_name.
The result of the growth column is calculated as follow : for each group of sensor_name, the int_value of each row is compared with the ext_value of the previous row, if no previous row then its ext_value is 0, if the int_value of the current row is higher than the ext_value of the previous row then the growth value increases of 1. If the current int_value is lower than the previous row ext_value then the growth remains at the same value than the previous value of growth.
In the example above,
for the very first row, 157 is compared with the previous row ext_value that doesn't exist so it's 0,
157 > 0 then growth value increase of 1 from 0.
on the 2nd row, 256 > 238 then growth = 1+1=2
on the 3rd row, 345 > 159 then growth = 2+1=3
on the 4th row, 861 < 895 then growth remains at the same previous value, so 3.
then the logic is re-applied to the second set of sensor_name :
1st row, 858 > 0 (because there is now previous row for this sensor_name) then growth = 1
2nd row, 588 < 904 then growth = 1
3rd row, 738 < 925 then growth = 1
4th row, 38 > 15 then growth = 1+1=2
I've tried by using the lag window over sensor_name partition but it doesn't give me correct result until now.
How can I solve this ?