In case anyone had the same question, this solved the issue for me, albeit an ugly implementation. A subquery can be created using subquery()
, and the columns accessed via subquery.c.{columnname}
# Subquery
sq = session.query(
Entity.name, Entity.filedate, func.sum(Entity.pnl).label("pnl"))
.filter(Entity.filedate >= datetime.date(2020,1,1))
.group_by(Entity.filedate, Entity.name)
).subquery()
session.query(sq.c.name, func.max(sq.c.pnl))
.group_by(sq.c.name).all()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…