Aggregate functions returning wrong results
See original GitHub issueWhat are you doing?
I have these two models:
const Post = sequelize.define('Post', {
title: DataTypes.TEXT('long'),
description: DataTypes.TEXT('long'),
body: DataTypes.TEXT('long'),
version: DataTypes.INTEGER,
type: DataTypes.STRING,
image: DataTypes.STRING,
});
Post.associate = function (models) {
models.Post.hasMany(models.Assessment, {as: 'PostAssessments'});
};
const Assessment = sequelize.define('Assessment', {
postCredibility: DataTypes.INTEGER,
body: DataTypes.TEXT('long'),
version: DataTypes.INTEGER,
isTransitive: DataTypes.BOOLEAN
});
I’m trying to execute this findAll query:
db.Post.findAll({
subQuery: false,
attributes: {
include: [
[db.sequelize.fn('MIN', db.sequelize.col('PostAssessments.postCredibility')), `minValidity`],
[db.sequelize.fn('MAX', db.sequelize.col('PostAssessments.postCredibility')), `maxValidity`],
[db.sequelize.fn('COUNT', db.sequelize.col('PostAssessments.id')), `count`]
]
},
include: [
{
model: db.Assessment,
as: 'PostAssessments',
required: true
}
],
order: [
[ 'updatedAt', 'DESC'],
[ 'PostAssessments', 'updatedAt', 'DESC'],
],
group: ['Post.id', 'PostAssessments.id']
})
What is happening?
min, max, and count functions don’t produce correct results. Below is an example output. The first entry has 2 “PostAssessments”, one with a “postCredibility” value of 1, and the other with a value of 2. However both the min and the max values are set to 1, and the count is also set to 1.
[{"minValidity":1,"maxValidity":1,"count":1,"PostAssessments":[{"id":472,"postCredibility":1,"body":null,"version":1,"isTransitive":false,"createdAt":"2019-04-14T18:56:06.000Z","updatedAt":"2019-04-14T18:56:07.000Z","PostId":457,"SourceId":7},{"id":467,"postCredibility":2,"body":null,"version":1,"isTransitive":null,"createdAt":"2019-04-14T18:43:38.000Z","updatedAt":"2019-04-14T18:43:38.000Z","PostId":457,"SourceId":9}]},{"minValidity":0,"maxValidity":0,"count":1,"PostAssessments":[{"id":471,"postCredibility":0,"body":null,"version":1,"isTransitive":false,"createdAt":"2019-04-14T18:55:43.000Z","updatedAt":"2019-04-14T18:55:44.000Z","PostId":456,"SourceId":7},{"id":466,"postCredibility":2,"body":null,"version":1,"isTransitive":null,"createdAt":"2019-04-14T18:43:38.000Z","updatedAt":"2019-04-14T18:43:38.000Z","PostId":456,"SourceId":9}]},{"minValidity":2,"maxValidity":2,"count":1,"PostAssessments":[{"id":468,"postCredibility":2,"body":null,"version":1,"isTransitive":null,"createdAt":"2019-04-14T18:43:38.000Z","updatedAt":"2019-04-14T18:43:38.000Z","PostId":458,"SourceId":9}]}]
Dialect: mysql __Sequelize version: 5.3.5
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
Aggregate functions return wrong values when joining more ...
Simple solution: Use two queries. Otherwise you can do your aggregated calculation in a subquery (on the whole table, not per row) then...
Read more >[SOLVED] AGGREGATE returns wrong results
Hi friends, the aggregate function should returns the minimum of specified range, for each cell in a column, but it returns wrong values....
Read more >GROUP BY gives wrong result with MIN() aggregate function
To solve this, simply create a temporary table (or subquerys), ordering the column to be used with MIN in ASC or DESC, according...
Read more >7 Common GROUP BY Errors - LearnSQL.com
1. Forgetting GROUP BY with Aggregate Functions. You use SELECT statements with the GROUP BY clause when you want to group and organize...
Read more >in 12.1, Aggregate Functions (e.g. Min Max) Return Bad ...
Querying a partitioned table with a unique index on the selected value with an aggregate function can return the wrong results. Cause. Sign...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I can confirm this is a valid bug.
This issue has been automatically marked as stale because it has been open for 14 days without activity. It will be closed if no further activity occurs within the next 14 days. If this is still an issue, just leave a comment or remove the “stale” label. 🙂