title: Redshift - Finding difference between rows for the same id
body: <p>I am trying to find the difference in value across rows and have built the below </p>
  
  <p>Data:</p>
  
  <pre><code>cust_id,sales
  cust_1,30
  cust_1,43
  cust_1,4
  cust_2,5
  cust_2,8
  </code></pre>
  
  <p>I have built the below:</p>
  
  <pre><code>select cust_id,sales, sales - lag(sales) over (order by cust_id) as change from table;
  </code></pre>
  
  <p>This returns the below output:</p>
  
  <pre><code>cust_id,sales, difference
  cust_1,30,13
  cust_1,43,-39
  cust_1,4,1
  cust_2,5,3
  cust_2,8,
  </code></pre>
  
  <p>How do I change the above output to only calculate difference if the cust_id is same and leave it blank if the cust_id changes. Using Redshift.</p>