【发布时间】:2017-04-30 18:15:54
【问题描述】:
我有一个表事务表,我尝试了这个,但它给出了数据库中存在的事务。
class transactions(models.Model):
amount=models.FloatField()
ts = models.DateTimeField(null=True)
revenueByMonth = transactions.objects.annotate(month=TruncMonth('ts')).values('month').annotate(c=Sum('amount')).values('month', 'c')
它会给出:
<[
{'c': 353.13, 'month': datetime.datetime(2016, 5, 1, 0, 0, tzinfo=<UTC>)},
{'c': 706.26, 'month': datetime.datetime(2016, 8, 1, 0, 0, tzinfo=<UTC>)},
{'c': 353.13, 'month': datetime.datetime(2016, 9, 1, 0, 0, tzinfo=<UTC>)},
{'c': 706.26, 'month': datetime.datetime(2016, 10, 1, 0, 0, tzinfo=<UTC>)},
{'c': 353.13, 'month': datetime.datetime(2016, 11, 1, 0, 0, tzinfo=<UTC>)},
{'c': 756.26, 'month': datetime.datetime(2016, 12, 1, 0, 0, tzinfo=<UTC>)}
]>
我想要每个月的金额总和,如果一个月没有交易,那么它应该返回零。所以请帮助我。
【问题讨论】: