I want the "Group by and Count" command in sqlalchemy. How can I do this?
If you use Table.query properties:
Table.query
from sqlalchemy import func Table.query.with_entities(Table.column, func.count(Table.column)).group_by(Table.column).all()
If you use the session.query() method (as described in miniwark's answer):
session.query()
from sqlalchemy import func session.query(Table.column, func.count(Table.column)).group_by(Table.column).all()
Count document indicates that for group_by queries it is best to use func.count():
group_by queries it is best to use func.count():
func.count()
If you use
Table.query
properties:If you use the
session.query()
method (as described in miniwark's answer):Count document indicates that for
group_by queries it is best to use
func.count()
: