Improve operation query.

This commit is contained in:
Alexis Lahouze 2015-07-17 14:32:47 +02:00
parent 6dbe266bd5
commit edade3cda7

View File

@ -58,19 +58,28 @@ class Operation(db.Model):
cls, cls,
case( case(
whens={cls.canceled: None}, whens={cls.canceled: None},
else_=func.sum(cls.value).over( else_=func.sum(
partition_by="canceled", cls.value
order_by=["operation_date", desc("value"), desc("label")]) ).over(
partition_by=[cls.account_id, cls.canceled],
order_by=["operation_date", desc("value"), desc("label")]
)
).label("sold") ).label("sold")
).filter(cls.account_id == account_id).order_by(
desc(cls.operation_date),
cls.value,
cls.label,
).subquery() ).subquery()
query = session.query(base_query).select_from(base_query) query = session.query(
query = query.filter(base_query.c.operation_date >= str(begin), base_query
base_query.c.operation_date <= str(end)) ).select_from(
base_query
).filter(
base_query.c.account_id == account_id,
base_query.c.operation_date >= str(begin),
base_query.c.operation_date <= str(end)
).order_by(
desc(base_query.c.operation_date),
base_query.c.value,
base_query.c.label,
)
return query return query