Categories
JavaScript Answers

How to do group by in Sequelize and JavaScript?

Spread the love

Sometimes, we want to do group by in Sequelize and JavaScript.

In this article, we’ll look at how to do group by in Sequelize and JavaScript.

How to do group by in Sequelize and JavaScript?

To do group by in Sequelize and JavaScript, we call findAll with an object that has the group property.

For instance, we write

const result = await Table.findAll({
  attributes: ["column1", sequelize.fn("count", sequelize.col("column2"))],
  group: ["Table.column1"],
});

to call Table.findAll with an object that has group= set to ["Table.column1"] to group by Table‘s column1.

Then we use await to get the returned result.

Conclusion

To do group by in Sequelize and JavaScript, we call findAll with an object that has the group property.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *