title: Postgresql: Identify events across multiple rows (power consumption)
body: <p>I've got a Postgresql table that contains time series data of accumulated power usage and the actual current at a time. At the moment washing a washing machine is logged which looks like this:</p>
  
  <p><a href="https://www.dropbox.com/s/5pf0cq6d2lmppvq/power.png?dl=0" rel="nofollow noreferrer">https://www.dropbox.com/s/5pf0cq6d2lmppvq/power.png?dl=0</a></p>
  
  <p><strong>Red</strong>: electrical current/power at this moment
  <strong>Green</strong>: Accumulated power</p>
  
  <p><strong>Goal</strong>: Identify cycles and return their starting time and power consumption.</p>
  
  <p>A washing <strong>cycle can be identified</strong> by the connected spikes in the <strong>red line</strong>.
  The <strong>power usage</strong> of a single cycle is the difference of accumulated.start_of_cycle and accumulated.end_of_cycle</p>
  
  <p>Can anyone tell me how I would do this? I assume window functions but I never did anything like this..</p>
  
  <p>A data sample can be found here: <a href="https://dl.dropboxusercontent.com/u/311345/data.csv" rel="nofollow noreferrer">https://dl.dropboxusercontent.com/u/311345/data.csv</a></p>