def get_recordcnts_by_machine_type(): q = db.session.query(Record.machine, db.func.count(Record.id).label('total')) q = q.group_by(Record.machine) q = q.order_by(desc('total')) return q.all()