title: MySQL: How to group data per hour and get the latest hour
body: <p>I'm trying to do a query that fetches data per <code>hour</code> but instead of the normal group by hour I want to narrow it down and only get the <code>latest</code> hour - meaning the newest data within that hour. With the picture shown below what I wanted to get is the rows with red boxes. If you will notice, first red row is <code>10:59:51</code> which means it's the only row that's within <code>10:00:00</code> and <code>10:59:59</code>. For the rest of the rows that is on <code>12:00</code> and above I wanted to get <code>12:37:14</code> because it's the latest or newest for that hour range.</p>
  
  <p><img src="https://i.stack.imgur.com/8yhFw.png" alt="enter image description here"></p>
  
  <p>I have a simple query that groups the data by <code>hour</code> using <code>HOUR()</code> like:</p>
  
  <pre><code>SELECT userid, username, date_created
  FROM user_accounts 
  WHERE date_created &gt;= '2009-10-27 00:00:00' AND date_created &lt; '2009-10-27 23:59:59'
  GROUP BY HOUR(date_created)
  </code></pre>
  
  <p>The query, however, is just grouping it by hour <code>10</code> and <code>12</code> which returns id <code>24</code> and <code>25</code> - but what I needed is <strong>id</strong> <code>24</code> and <code>28</code>. Any ideas?</p>