Grouping and Aggregating Records in Sequelize
Sequelize·3 min read·Jan 1, 2025
In SQL, grouping and aggregation are used to summarize and analyze data.
Grouping allows us to group records that share a common column, while aggregation functions such as COUNT, SUM, AVG, MAX, and MIN allow us to perform calculations on groups of data.
Aggregation functions
To execute an aggregation function like COUNT or SUM on an column, you can use the Sequelize.fn() static method:
{ attributes: [ [Sequelize.fn(<function>, Sequelize.col(<attribute>)), <alias>?] ]}Where:
-
functionis a string containing the name of the aggregation function. -
attributeis a string containing the name of the column to perform an aggregation on.
Unlock the Build RESTful APIs in Node.js module
Learn how to build real database-backed and framework-powered RESTful APIs with MySQL, Sequelize, and Express.
You get immediate access to:
- 37 focused lessons across MySQL, Sequelize, backend, and Express
- 4 real-world projects with commented solutions
- Ongoing updates to this bundle
- Lifetime access to this bundle