Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a distinct count with sequelize?

Tags:

sequelize.js

I am trying to get a distinct count of a particular column using sequelize. My initial attempt is using the 'count' method of my model, however it doesn't look like this is possible.

The DISTINCT feature is needed because I am joining other tables and filtering the rows of the parent based on the related tables.

here's the query I would like:

SELECT COUNT(DISTINCT Product.id) as `count`  FROM `Product`  LEFT OUTER JOIN `Vendor` AS `vendor` ON `vendor`.`id` = `Product`.`vendorId`  WHERE (`vendor`.`isEnabled`=true ); 

using the following query against my Product model:

Product.count({         include: [{model: models.Vendor, as: 'vendor'}],         where: [{ 'vendor.isEnabled' : true }]     }) 

Generates the following query:

SELECT COUNT(*) as `count`  FROM `Product`  LEFT OUTER JOIN `Vendor` AS `vendor` ON `vendor`.`id` = `Product`.`vendorId`  WHERE (`vendor`.`isEnabled`=true ); 
like image 793
Adam Avatar asked Mar 25 '14 18:03

Adam


People also ask

How do I get a distinct count in Sequelize?

The following code will give you the amount of different values in MyModel 's someColumn : MyModel. count({distinct: 'someColumn', where: {...}}) . then(function(count) { // count is an integer });

How do I use Groupby Sequelize?

In Sequelize, you can add the group option in your query method findAll() to add the GROUP BY clause to the generated SQL query. Now you want to select all firstName values and group any duplicate values of the column. Here's the code for calling the findAll() method on the model: const users = await User.

Where is Sequelize?

Sequelize where option accepts an object describing the WHERE clause to add to your generated SQL query. For a WHERE clause with a simple condition, you can add a single property to the where object. The property name will be the column name and the property value will be the value you use to filter the query.


2 Answers

UPDATE: New version

There are now separate distinct and col options. The docs for distinct state:

Apply COUNT(DISTINCT(col)) on primary key or on options.col.

You want something along the lines of:

MyModel.count({   include: ...,   where: ...,   distinct: true,   col: 'Product.id' }) .then(function(count) {     // count is an integer }); 

Original Post

(As mentioned in the comments, things have changed since my original post, so you probably want to ignore this part.)

After looking at Model.count method in lib/model.js, and tracing some code, I found that when using Model.count, you can just add any kind of aggregate function arguments supported by MYSQL to your options object. The following code will give you the amount of different values in MyModel's someColumn:

MyModel.count({distinct: 'someColumn', where: {...}}) .then(function(count) {     // count is an integer }); 

That code effectively generates a query of this kind: SELECT COUNT(args) FROM MyModel WHERE ..., where args are all properties in the options object that are not reserved (such as DISTINCT, LIMIT and so on).

like image 109
Domi Avatar answered Sep 27 '22 20:09

Domi


Looks like this is now supported in Sequelize versions 1.7.0+.

the count and findAndCountAll methods of a model will give you 'real' or 'distinct' count of your parent model.

like image 39
Adam Avatar answered Sep 27 '22 20:09

Adam