title: How to calculate the sum of a column in an SQL table for a given date and patching missed values with those of the closest previous date?
body: <p>I have a table that seems like this:</p>
  
  <pre><code>+-----+-----------+------------+
  | id  |     value |       date |
  +-----+-----------+------------+
  | id1 |      1499 | 2012-05-10 |
  | id1 |      1509 | 2012-05-11 |
  | id1 |      1511 | 2012-05-12 |
  | id1 |      1515 | 2012-05-13 |
  | id1 |      1522 | 2012-05-14 |
  | id1 |      1525 | 2012-05-15 |
  | id2 |      2222 | 2012-05-10 |
  | id2 |      2223 | 2012-05-11 |
  | id2 |      2238 | 2012-05-13 |
  | id2 |      2330 | 2012-05-14 |
  | id2 |      2340 | 2012-05-15 |
  | id3 |      1001 | 2012-05-10 |
  | id3 |      1020 | 2012-05-11 |
  | id3 |      1089 | 2012-05-12 |
  | id3 |      1107 | 2012-05-13 |
  | id3 |      1234 | 2012-05-14 |
  | id3 |      1556 | 2012-05-15 |
  | ... |       ... |        ... |
  | ... |       ... |        ... |
  | ... |       ... |        ... |
  +-----+-----------+------------+
  </code></pre>
  
  <p>What I want to do is to produce the total sum of the <code>value</code> column for all the data
  in this table per date. There is one entry for each <code>id</code> per day. The problem is that
  some ids haven't a value for all days, e.g. <strong>id2 haven't a value for the date: 2012-05-11</strong></p>
  
  <p>What I want to do is: when for a given date there is no value for a specific id, then
  the value of the previous date (much closer to the given date) to be calculated in the sum.</p>
  
  <p>For example, suppose we have only the data shown above. we can take the sum of all values
  for a specific date from this query:</p>
  
  <pre><code>SELECT SUM(value) FROM mytable WHERE date='2012-05-12';
  </code></pre>
  
  <p>the result will be: 1511 + 1089 = <strong>2600</strong></p>
  
  <p>But what I want to have is to make a query that does this calculation:
  1511 + <strong>2223</strong> + 1089 = <strong>4823</strong></p>
  
  <p>so that the <strong>2223</strong> of <code>id2</code> of date 2012-05-11 is added instead of the <em>missed</em> value:</p>
  
  <pre><code>| id2 |  2223 | 2012-05-11 |
  </code></pre>
  
  <p>Do you know how can I do this through an SQL query? or through a script? e.g. python..</p>
  
  <hr>
  
  <p>I have thousands of ids per date, so I would like the query to be a little bit fast if it is possible.</p>