title: Selecting most recent data from table
body: <p>I have a table setup called <code>item_histories</code>, which is setup like below.</p>
  
  <pre><code>+--------------------------------+
  |  id  itemId    date    price   |
  +--------------------------------+
  | 1    2    2015-01-10    310    |
  | 2    2    2015-01-11    318    |
  | 3    2    2015-01-12    326    |
  | 4    2    2015-01-13    333    |
  | 5    2    2015-01-14    335    |
  | 6    6    2015-01-10    121598 |
  | 7    6    2015-01-11    121598 |
  | 8    6    2015-01-12    122666 |
  | 9    6    2015-01-13    122666 |
  | 10   6    2015-01-14    122666 |
  +--------------------------------+
  </code></pre>
  
  <p>I'm trying to select the most recent row for each <code>itemId</code>, like so..</p>
  
  <pre><code>+--------------------------------+
  |  id  itemId    date    price   |
  +--------------------------------+
  | 5    2    2015-01-14    335    |
  | 10   6    2015-01-14    122666 |
  +--------------------------------+
  </code></pre>
  
  <p>I'm using <code>Sequelize</code> and this is the code I have now.</p>
  
  <pre><code>db.itemHistory.findAll({
      attributes: [
          'id',
          'price',
          'itemId',
          [db.Sequelize.fn('max', db.Sequelize.col('date')), 'date']
      ],
      group: ['itemId'],
      limit: 1
  }).then(function(histories) {
      console.log(histories);
  });
  </code></pre>
  
  <p>Which generated this query, <code>SELECT `id`, `price`, `itemId`, max(`date`) AS `test` FROM `item_histories` AS `itemHistory` GROUP BY `itemId` LIMIT 1</code>.</p>
  
  <p>The <code>MAX(`date`)</code> was my attempt at accomplishing this, but all it does is select the first row, but has a <code>date</code> of the most recent.</p>