title: Mysql - Group by and select max value
body: <p>I have the following tables:</p>
  
  <p><strong>reservas_tanatosalas</strong>
  <a href="https://i.stack.imgur.com/Yu8jY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Yu8jY.png" alt="enter image description here"></a></p>
  
  <p>I have the following query: </p>
  
  <pre><code>SELECT r.cod_reserva, MAX(r.hora_fin_utc) FROM reservas_tanatosalas r GROUP BY r.cod_tanatosala
  </code></pre>
  
  <p><strong>Results:</strong> Is showing the right max value but the wrong <code>cod_reserva</code>. Why?</p>
  
  <pre><code>cod_reserva  MAX(r.hora_fin_utc)
  7           9999999999
  6           9999999999
  </code></pre>
  
  <p><strong>What I want to get?:</strong></p>
  
  <pre><code>cod_reserva  MAX(r.hora_fin_utc)
  7           9999999999
  8           9999999999
  </code></pre>
  
  <h1>UPDATE</h1>
  
  <p>cod_reserva could change to varchar in the future hence is not an option to me use max on it.</p>