title: Selecting row with minimum date while summing another column
body: <p>I have below Table in MySQL:</p>
  
  <p><a href="https://i.stack.imgur.com/2e5cS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2e5cS.png" alt="enter image description here"></a></p>
  
  <p>I want to fetch sum of <code>count</code> column but the expiry date should be shown either null or the smallest date with <code>group by package_id</code>.</p>
  
  <p>I have below query currently:</p>
  
  <pre><code>select mt.id, mt.package_id, sum(mt.count), mt.expiry_date
  from my_table mt left join my_table mt2 on mt.id = mt2.id and mt.expiry_date &lt; mt2.expiry_date
  group by mt.package_id;
  </code></pre>
  
  <p>It gives below result:</p>
  
  <p><a href="https://i.stack.imgur.com/AcbNw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AcbNw.png" alt="enter image description here"></a></p>
  
  <p>My required result should have <code>2</code> as id in first row instead of <code>1</code> and the <code>expiry_date</code> should be minimum which is <code>2010-01-01 00:00:00</code>:</p>
  
  <p><a href="https://i.stack.imgur.com/40mXQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/40mXQ.png" alt="enter image description here"></a></p>