Group by & count function in sqlalchemy

Nazmul Hasan picture Nazmul Hasan · Jun 27, 2009 · Viewed 110.9k times · Source

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

Answer

miniwark picture miniwark · Nov 3, 2010

The documentation on counting says that for group_by queries it is better to use func.count():

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