Group by & count function in sqlalchemy

Viewed 171043

I want a "group by and count" command in sqlalchemy. How can I do this?

3 Answers

If you are using Table.query property:

from sqlalchemy import func
Table.query.with_entities(Table.column, func.count(Table.column)).group_by(Table.column).all()

If you are using session.query() method (as stated in miniwark's answer):

from sqlalchemy import func
session.query(Table.column, func.count(Table.column)).group_by(Table.column).all()
Related