title: SQL query to report duration untill next log entry
body: <p>I'm working with PostgreSql and trying to build reporting query for my logs, but unfortunately unsuccessfully...
  Basically I have LOG table which logs status changes of other entity. So for the sake of simplicity lets say it has columns STATUS and STATUS_CHANGE_DATE. Now each status change updates this logging table with new status and time it was changed. What I need is the duration and number of times status in it for each status (same status can be used multiple times, e.g go from status 1 to 2 then back to 1). I would like to build a view for it and use in my java application reporting by mapping that view right to hibernate entity. Unfortunately I'm not that experienced with sql so maybe someone can give me some hints of whats best solution would be as I tried few things but basically don't know how to do it.
  Lets say we have:</p>
  
  <pre><code>STATUS STATUS_CHANGE_DATE
  1      2013 01 01
  2      2013 01 03
  1      2013 01 06
  3      2013 01 07
  </code></pre>
  
  <p>My wanted result would be a table that contains status 1 with 2 times and 3 days duration and status 2 1 time with 3 days duration too (assuming status 3 is end(or close) and its duration is not required).</p>
  
  <p>Any ideas?</p>