title: sql window function to detect change in column values
body: <p>I'd like to detect changes in column values in this (example) db</p>
  
  <pre><code>    WITH events(id, row,event) AS (
      VALUES
      (1,1, 0 )
     ,(1,2, 0 ) 
     ,(1,3, 1 )
     ,(1,4, 0 )
     ,(1,5, 1 )
     ,(2,1, 0 )
     ,(2,2, 1 )
     ,(3,1, 0 )
     ,(3,2, 0 )
     )
     select * from events
  </code></pre>
  
  <p>What I am looking for is code for a new column 'code' which switches to 1 AFTER
  de event column shows a 1.  Within the same id the code stays 1.
  For this example this new column wil look like this</p>
  
  <pre><code>    WITH events2(id, row,event, code) AS (
      VALUES
      (1,1, 0, 0 )
     ,(1,2, 0, 0 ) 
     ,(1,3, 1, 0 )
     ,(1,4, 0, 1 ) -- notice the switch here
     ,(1,5, 1, 1 ) -- 
     ,(2,1, 0, 0 )
     ,(2,2, 1, 0 )
     ,(3,1, 0, 0 )
     ,(3,2, 0, 0 )
     )
     select * from events2
  </code></pre>
  
  <p>I have a hunch that the answer will be related to the answer  on this question : <a href="https://stackoverflow.com/questions/22512335/postgresql-window-function-partition-by-comparison">PostgreSQL window function: partition by comparison</a></p>
  
  <p>Somehow I cannot figure this out myself..</p>
  
  <p>Peter</p>