title: Sequelize - Get max date for a group of objects
body: <p>I want to get the max (most recent) created at date of each group of objects from a table using sequelize.</p>
  
  <p>My entity roughly follows this interface:</p>
  
  <pre><code>class MyEntity {
      id,
      groupName,
      createdAt
  }
  </code></pre>
  
  <p>I want to do something like this:</p>
  
  <pre><code>await MyEntity.findAll({
        attributes: [[Sequelize.fn('max', Sequelize.col('created_at')), 'max']],
        group: ['group_name']
      })
  </code></pre>
  
  <p>I would interpret this as:
  1. For all entites
  2. Group by "groupName"
  3. And get the max value for each group</p>
  
  <p>However, I get the following error:</p>
  
  <blockquote>
    <p>SequelizeDatabaseError: column "Template.created_at" must appear in the GROUP BY clause or be used in an aggregate function</p>
  </blockquote>
  
  <p>I definitely do not want to group by the <code>created_at</code> column as well, as that would be meaningless.</p>
  
  <p>The SQL for this operation seems pretty basic:</p>
  
  <pre><code>SELECT groupName, MAX(createdAt) FROM [MyEntity]
  GROUP BY groupName
  </code></pre>