I want the "Group by and Count" command in sqlalchemy. How can I do this?
P粉1808446192023-10-16 09:16:22
If you use Table.query
properties:
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):
from sqlalchemy import func session.query(Table.column, func.count(Table.column)).group_by(Table.column).all()
P粉0709187772023-10-16 00:27:11
Count document indicates that for group_by< /code> queries it is best to use
func.count()
:
from sqlalchemy import func session.query(Table.column, func.count(Table.column)).group_by(Table.column).all()