Python SQLAlchemy子查询-总和的平均值

Python SQLAlchemy子查询-总和的平均值,python,sqlalchemy,Python,Sqlalchemy,有没有办法在SQLAlchemy ORM中编写以下SQL语句: SELECT AVG(a1) FROM (SELECT sum(irterm.n) AS a1 FROM irterm GROUP BY irterm.item_id); 谢谢为什么要在sums.c.a1查询中而不是sums.a1查询?我不知道是什么原因effect@Guidosums是子查询对象,而不是映射类。它通过c和columns属性公开其可用列。为什么要在查询sums.c.a1中而不是sums.a1中写入?我不知道是什么原

有没有办法在SQLAlchemy ORM中编写以下SQL语句:

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

谢谢

为什么要在sums.c.a1查询中而不是sums.a1查询?我不知道是什么原因effect@Guido
sums
是子查询对象,而不是映射类。它通过
c
columns
属性公开其可用列。为什么要在查询sums.c.a1中而不是sums.a1中写入?我不知道是什么原因effect@Guido
sums
是子查询对象,而不是映射类。它通过
c
columns
属性公开其可用列。
sums = session.query(func.sum(Irterm.n).label('a1')).group_by(Irterm.item_id).subquery()
average = session.query(func.avg(sums.c.a1)).scalar()