title: LAG function and GROUP BY
body: <p>I have a table like this, </p>
  
  <pre><code> event_id |          date          
  ----------+------------------------
    1703702 | 2013-06-25 07:50:57-04
    3197588 | 2013-06-25 07:51:57-04
   60894420 | 2013-06-25 07:52:57-04
   60894420 | 2013-06-25 07:53:57-04
     183503 | 2013-06-25 07:54:57-04
   63116743 | 2013-06-25 07:55:57-04
   63110451 | 2013-06-25 07:56:57-04
   63116743 | 2013-06-25 07:57:57-04
   63116743 | 2013-06-25 07:58:57-04
  </code></pre>
  
  <p>I'd like to apply the lag function but also a group by so I can find the time intervals between any particular event_id.</p>
  
  <p>I'd like something like this:</p>
  
  <pre><code>SELECT event_id, difference
  FROM ( 
    SELECT event_id, date - lag(date) over (order by date) as
    difference FROM table GROUP BY event_id
  ) t;
  </code></pre>
  
  <p>I cannot however use GROUP BY with the LAG function. I'd like a result similar to the following:</p>
  
  <pre><code>63116743, {120, 60}
  60894420, {60}
  ...
  ...
  </code></pre>
  
  <p>So there was a 120s and 60s window between the events for the first id, and a 60s window for the second id.</p>
  
  <p>Is there a way to do this? The output format is not too important as long as I can get it into an array in the end. I'm using Postgres 9.1</p>