Home > Software engineering >  How to write a count like this in Sequelize?
How to write a count like this in Sequelize?

Time:10-25

SELECT COUNT(id), age FROM red_cross_volunteers GROUP BY age;

CodePudding user response:

Simply use Sequelize.fn in attributes option and group option while calling findAll:

const stats = await RedCrossVolunteers.findAll({
  attributes: [[Sequelize.fn('COUNT', Sequelize.col('id')), 'age_count'], 'age'],
  group: ['age']
})
  • Related