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,
case(
whens={cls.canceled: None},
else_=func.sum(cls.value).over(
partition_by="canceled",
order_by=["operation_date", desc("value"), desc("label")])
else_=func.sum(
cls.value
).over(
partition_by=[cls.account_id, cls.canceled],
order_by=["operation_date", desc("value"), desc("label")]
)
).label("sold")
).filter(cls.account_id == account_id).order_by(
desc(cls.operation_date),
cls.value,
cls.label,
).subquery()
query = session.query(base_query).select_from(base_query)
query = query.filter(base_query.c.operation_date >= str(begin),
base_query.c.operation_date <= str(end))
query = session.query(
base_query
).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