title: Query to get the latest data of a group by data in a MySQL table
body: <p>I have a SQL table where I am trying to extract the latest data for each category.</p>
  
  <p>My table schema</p>
  
  <pre><code>| deviceId                    | varchar(30) | NO   |     | NULL    |       |
  | rssi                        | varchar(15) | YES  |     | NULL    |       |
  | space_temperature           | varchar(30) | YES  |     | NULL    |       |
  | control                     | varchar(30) | YES  |     | NULL    |       |
  | counter                     | varchar(30) | YES  |     | NULL    |       |
  | timestamp                   | varchar(30) | NO   |     | NULL    |       |
  | date                        | varchar(30) | NO   |     | NULL    |       |
  | floor                       | varchar(30) | YES  |     | NULL    |       |
  | Id                          | varchar(5)  | YES  |     | NULL    |       |
  </code></pre>
  
  <p>I want to fetch the latest row for each Id.</p>
  
  <pre><code>select * from Data where floor = 'L2' and date = '2019-12-10' group by Id; 
  </code></pre>
  
  <p>This is giving me a row for each Id, but it is giving me the first entry but not the latest entry in the table. </p>
  
  <p>I tried with </p>
  
  <pre><code>select *,max(timestamp) as latest_timestamp from Data where floor = 'L2' and date = '2019-12-10' group by Id; 
  </code></pre>
  
  <p>This is giving the latest timestamp but the other columns have the data of the first entry. How to get the results of the latest entry for each Id group?</p>