【发布时间】:2020-09-12 02:09:31
【问题描述】:
我有 SQL 查询,我需要在 Django 中使用 ORM 技术。
SELECT a.id, a.amount, SUM(b.amount)
FROM cashflow_statement a, cashflow_statement b
WHERE b.id <= a.id GROUP BY a.id
ORDER BY a.id
编辑: Django 模型
class Statement(models.Model):
amount = models.DecimalField(max_digits=10, decimal_places=2)
date = models.DateField()
def __str__(self):
return self.title
输入数据
id, amount, date
1. 10 25/06/2020
2. -10 25/06/2020
3. 40 25/06/2020
4. 10 25/06/2020
5. -30 25/06/2020
6. 10 25/06/2020
Need Output:
id, amount, sum
1. 10. 10
2. -10. 0
3. 40. 40
4. 10. 50
5. -30. 20
6. 10. 30
【问题讨论】:
-
请分享相关模型。
-
@WillemVanOnsem 添加了模型并输出我需要的东西,上面的 Sql 给出了相关的输出。
标签: sql django postgresql django-models django-orm