How does group by works in sequelize? How does group by works in sequelize? javascript javascript

How does group by works in sequelize?


I think you looking for something like this:

 Table.findAll({   attributes: ['column1',      sequelize.fn('count', sequelize.col('column2'))],    group: ["Table.column1"] }).success(function (result) { });

Update: Newer versions of Sequelize uses .then instead of .success.

 Table.findAll({   attributes: ['column1',      sequelize.fn('count', sequelize.col('column2'))],    group: ["Table.column1"] }).then(function (result) { });


Group by with count sequelize ORM

'field' - you can rename this string, it's your field(column) in database

'count' - reserved string need for get count in sequelize

'cnt' - you can rename this string, it's your output count

Sequelize version 3.25.0

User.findAll({      attributes: ['field', [sequelize.fn('count', sequelize.col('field')), 'cnt']],      group: ['field'],})