SELECT ID, DAY, VALUE, (LAG(comm_sum, 0,0) OVER (PARTITION BY ID ORDER BY DAY ASC)) - (LAG(comm_sum, 1,0) OVER (PARTITION BY ID ORDER BY DAY ASC)) AS CAHSBACK FROM (SELECT ID, DAY, VALUE,
(SUM(VALUE) OVER (PARTITION BY ID ORDER BY DAY ASC))/1000 AS comm_sum FROM task3)AS a;