title: MySQL return last value per day
body: <p>I am having the following dataset:</p>
  
  <pre><code>+----+------------+-------+-------+
  | ID |    Date    | Time  | Value |
  +----+------------+-------+-------+
  |  1 | 2015-01-01 | 14:00 |     1 |
  |  2 | 2015-01-01 | 16:00 |     2 |
  |  3 | 2015-01-02 | 14:00 |    15 |
  |  3 | 2015-01-02 | 17:00 |    12 |
  +----+------------+-------+-------+
  </code></pre>
  
  <p>Now I want to return the last value for each day and the result should look like that:</p>
  
  <pre><code>+----+------------+-------+-------+
  | ID |    Date    | Time  | Value |
  +----+------------+-------+-------+
  |  2 | 2015-01-01 | 16:00 |     2 |
  |  3 | 2015-01-02 | 17:00 |    12 |
  +----+------------+-------+-------+
  </code></pre>
  
  <p>I am using the following code that somehow delivers close results:</p>
  
  <pre><code>SELECT DISTINCT     t.id,
                      t.date,
                      t.time,
                      t.balance
  FROM db1 t
  JOIN (
        SELECT MAX(tt.time) 'maxtime' 
        FROM db1 tt
        GROUP BY tt.date) m ON m.maxtime = t.time
  </code></pre>
  
  <p>This now returns the desired last value for a majority of the dates. However, this code also deliveres some random lines that are not the last value.</p>
  
  <p>Many thanks already now for your help,
  cheers</p>