title: Finding the difference between the latest and the second latest term
body: <p>The structure of the table is like </p>
  
  <pre><code>create table events(
    event_type integer not null,
    value integer not null,
    time timestamp not null,
    unique (event_type ,time)
    );
  </code></pre>
  
  <p>Have inserted few values like </p>
  
  <pre><code>insert into events values
  (2,   5,  '2015-05-09 12:42:00'),
  (4, -42,  '2015-05-09 13:19:57'),
  (2,   2,  '2015-05-09 14:48:39'),
  (2,   7,  '2015-05-09 13:54:39'),
  (3,  16,  '2015-05-09 13:19:57'),
  (3,  20,  '2015-05-09 15:01:09') 
  </code></pre>
  
  <p>I want to write a query that for each event_type that has been registered more than once returns the difference between the latest and the second latest value .</p>
  
  <p>Given the above data, the output should be like </p>
  
  <pre><code>event_type value
  2          -5
  3           4
  </code></pre>
  
  <p>I can bring out the lowest and the highest using below queries . This is how far I could reach ..confused on the way further . </p>
  
  <pre><code>select event_type,value,time from events group by event_type order by event_type desc;
  
  select * from events group by event_type order by event_type ;
  </code></pre>