Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLAlchemy subquery - average of sums

is there any way how to write the following SQL statement in SQLAlchemy ORM:

SELECT AVG(a1) FROM (SELECT sum(irterm.n) AS a1 FROM irterm GROUP BY irterm.item_id);

Thank you

like image 441
honzas Avatar asked Feb 16 '10 13:02

honzas


1 Answers

sums = session.query(func.sum(Irterm.n).label('a1')).group_by(Irterm.item_id).subquery()
average = session.query(func.avg(sums.c.a1)).scalar()
like image 130
Ants Aasma Avatar answered Oct 06 '22 19:10

Ants Aasma