Reputation: 11931
How would I get COUNT and following columns group by email order by desc using sequelize. I am getting following error : not a valid attribute definition.
const data = await NominationModel.findAll({attributes: ['email', 'nomineename',[sequelize.fn('COUNT', sequelize.col('email'))]] });
I am trying to get below sql query using sequelize.
SELECT COUNT(1),nomineename, email FROM devchoice.nominations GROUP BY (email) order by email desc
Upvotes: 0
Views: 1620
Reputation: 600
You seem to miss the group
in sequelize, try this:
NominationModel.findAll({
group: ['email', 'nomineename'],
attributes: ['email', 'nomineename', [sequelize.fn('COUNT', sequelize.col('email')), 'count']],
order: [
[Sequelize.literal('count'), 'DESC']
],
raw: true
}).then((rst) => {
console.log('rst' ,rst);
}).catch((err) => {
console.log('err', err);
})
Upvotes: 2