1

I'm trying to imitate such query in sqlalchemy: SELECT col1, COUNT(col1) FROM "table" (simplified case)

According to this answer, I found how to count a single column, but not a column - COUNT(column) pair.

The answer from a link: session.query(MyTable.col1).count()

My current case: db.query(models.User.places).filter(models.User.id == 10).all() (Also want to get a count of places)

1 Answer 1

1

This will count the occurrence of places alongside the places value, grouped by User id:

from sqlalchemy import func

cs.query(User.id, User.places, func.count(User.places)).group_by(User.id).all()

Is that what you are after?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.