title: PostgreSQL sum of intervals
body: <p>In my database I have rows like:</p>
  
  <pre><code>date    , value
  16:13:00, 500
  16:17:00, 700
  16:20:00, 0
  </code></pre>
  
  <p>Now I want to do "special sum" over value from 16:00:00 to 17:00:00. So until 16:13 we assume that we have 0.
  So special sum would look like (I'll omit seconds):</p>
  
  <pre><code>...
  0 +     -- (16:12)
  500 +   -- (16:13)
  500 +   -- (16:14)
  500 +   -- (16:15)
  500 +   -- (16:16)
  700 +   -- (16:17)
  700 +   -- (16:18)
  700 +   -- (16:19)
  0 +     -- (16:20)
  ...
  </code></pre>
  
  <p>So I have in database only changes of value and when this change occurs. And I want to sum over the whole hour. Result of this should be 4100.
  What is the optimal way of doing that kind of sum in sql with PostgreSQL?</p>
  
  <p>Best</p>