Return rows only if they are different that previous days value

You could try using the Lead() and Lag() SQL Analytic function for this

SELECT *
from (SELECT *, LAG(AMOUNT) over (partition by user_id order by datetime) as Yesterday_AMOUNT
      from SALES s
     ) x
where cast(s.datetime as date) = cast(getdaate() as date) and 
       x.AMOUNT <> s.Yesterday_AMOUNT;

Leave a Comment

tech