select `appointment`.`user_id`, count(`job`.`done100`) from `appointment`, `job` where `appointment`.`job_id`=`job`.`id` AND `job`.`done100`=0 group by `appointment`.`user_id` order by `appointment`.`user_id`
class Job(object): pass class Appointment(object): pass mapping(Job, job_table) mapping(Appointment, appointment_table) self.session = getsession() result = self.session.query(Job, Appointment).group_by(Appointment.c.user_id).select(and_(Job.c.id==Appointment.c.job_id, Job.c.done100==0))
пробовал еще вот так
sql = select([Appointment.c.user_id.label('user_id'), func.count(Job.c.done100).label('count')]).group_by(Appointment.c.user_id) self.session.execute(sql)