title: How to prevent arithmetic on null values?
body: <p>Using Oracle11g:</p>
  
  <p>If the column is null, I want to exclude the arithmetic on the column.</p>
  
  <p>In this case, how can I get <strong>lag_diff</strong> for <strong>seqno=1</strong> to be <strong>null</strong>?</p>
  
  <pre><code>   with detail_records as (
      select 1 seqno, 10 set_a  from dual union all
      select 2 seqno, 10 set_a  from dual union all
      select 3 seqno, 20 set_a  from dual union all
      select 4 seqno, 30 set_a  from dual )
    select  seqno,
            set_a,
            lag_val,
      case (lag_val - set_a)
      when 0 then 'AAA'
      else  'BBB'
      end  as lag_diff
   from ( select seqno, 
          set_a, 
          lag(set_a,1) over (order by seqno) as lag_val       
    from detail_records)
  order by seqno
  </code></pre>
  
  <p><strong>Desired Results</strong></p>
  
  <pre><code>  SEQNO  SET_A LAG_VAL LAG_DIFF
    ----------------------------
    1       10  &lt;NULL&gt;   &lt;null&gt;
    2       10   10       AAA
    3       20   10       BBB
    4       30   20       BBB
  </code></pre>