title: Lag function not working properly
body: <p>I got this query</p>
  
  <pre><code>CREATE OR REPLACE View  QTY_VIEW  As 
  SELECT       
    DATA_VIEW.RAW_ID as RAW_ID, 
    DATA_VIEW.CREATE_DATE as CREATE_DATE, 
    DATA_VIEW.UPDATE_DATE as UPDATE_DATE, 
    DATA_VIEW.INITIAL_QTY as INITIAL_QTY, 
    DATA_VIEW.ACTUAL_QTY as ACTUAL_QTY, 
    DATA_VIEW.USED_QTY as USED_QTY, 
    DATA_VIEW.LAST_USED_BY as LAST_USED_BY, 
    CASE 
      WHEN DATA_VIEW.ACTUAL_QTY = DATA_VIEW.INITIAL_QTY 
        THEN 0             
      WHEN LAG(DATA_VIEW.USED_QTY, 1, 0) 
             OVER (ORDER BY DATA_VIEW.RAW_ID ASC,DATA_VIEW.ACTUAL_QTY,DATA_VIEW.UPDATE_DATE)
             = DATA_VIEW.USED_QTY - DATA_VIEW.USED_QTY   
        THEN 0            
      ELSE
        LAG(DATA_VIEW.USED_QTY, 1, 0) 
          OVER (ORDER BY DATA_VIEW.RAW_ID ASC,DATA_VIEW.ACTUAL_QTY,DATA_VIEW.UPDATE_DATE)
        - DATA_VIEW.USED_QTY  
      END as SINGLE_USE_QTY 
  FROM 
    DATA_VIEW 
  order by RAW_ID asc, UPDATE_DATE,ACTUAL_QTY ; 
  </code></pre>
  
  <p>I think i am not using the LAG function properly as the last result is a negative value instead of a positive one. Can you help me please with some advice?
  The DATA_VIEW is
  <a href="https://i.stack.imgur.com/CgsAL.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CgsAL.jpg" alt="DATA_VIEW"></a>
  And QTY_VIEW is like that<a href="https://i.stack.imgur.com/IgahN.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IgahN.jpg" alt="QTY_VIEW"></a>
  What i did wrong?</p>