Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filtering by Count() in SQlite raises an error #456

Closed
arski opened this issue Nov 1, 2014 · 3 comments
Closed

Filtering by Count() in SQlite raises an error #456

arski opened this issue Nov 1, 2014 · 3 comments

Comments

@arski
Copy link

arski commented Nov 1, 2014

What I'm trying to do is something like:

User.select(User, fn.Count(Tweet)).join(Tweet).where(fn.Count(Tweet) > 0)

Sadly, in SQlite, this throws an OperationalError: misuse of aggregate: Count() - which upon further investigation on SO turns out to be a specific SQlite issue where filtering by aggregates has to be done inside HAVING clause instead of WHERE - http://stackoverflow.com/questions/648083/sql-error-misuse-of-aggregate - any chance you could look into this please

@coleifer
Copy link
Owner

coleifer commented Nov 1, 2014

Your query is incorrect.

(User
 .select(User, fn.COUNT(Tweet.id).alias('count'))
 .join(Tweet)
 .group_by(User)
 .having(fn.COUNT(Tweet.id) > 0))

@coleifer coleifer closed this as completed Nov 1, 2014
@coleifer
Copy link
Owner

coleifer commented Nov 1, 2014

When aggregating, you can only filter on aggregates in the HAVING clause. Additionally, you need to tell the database how to group the rows -- in this case, by user.

@arski
Copy link
Author

arski commented Nov 2, 2014

My bad, I thought I had filtered by SUM or COUNT in a WHERE clause in MySQL before.. but maybe that was an erroneous recollection. thanks for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants