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)

salius
  • 918
  • 1
  • 14
  • 30

1 Answers1

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?

CodeMantle
  • 1,249
  • 2
  • 16
  • 25