如何在 Django 中使用 postgresql 'interval'?
How to use posgresql 'interval' in Django?
这是我的 PostgreSQL 语句。
select round(sum("amount") filter(where "date">=now()-interval '12 months')/12,0) as avg_12month from "amountTab"
如何在 Django 中使用它?
我有一个名为 'Devc' 的对象,其属性为 'date'。
我想获取过去 12 个月内特定数据的总和,而不是过去 365 天。
您可以试试这个来获取 past 12 months
中的数据。
today= datetime.now()
current_month_first_day = today.replace(day = 1)
previous_month_last_day = current_month_first_day - timedelta(days = 1)
past_12_month_first_day = previous_month_last_day - timedelta(days = 360)
past_12_month_first_day = past_12_month_first_day.replace(day = 1)
past_12_month_avg = Devc.objects.filter(date__range=(past_12_month_first_day,current_month_first_day)).aggregate(Sum('amount'))['amount']
这是我的 PostgreSQL 语句。
select round(sum("amount") filter(where "date">=now()-interval '12 months')/12,0) as avg_12month from "amountTab"
如何在 Django 中使用它?
我有一个名为 'Devc' 的对象,其属性为 'date'。
我想获取过去 12 个月内特定数据的总和,而不是过去 365 天。
您可以试试这个来获取 past 12 months
中的数据。
today= datetime.now()
current_month_first_day = today.replace(day = 1)
previous_month_last_day = current_month_first_day - timedelta(days = 1)
past_12_month_first_day = previous_month_last_day - timedelta(days = 360)
past_12_month_first_day = past_12_month_first_day.replace(day = 1)
past_12_month_avg = Devc.objects.filter(date__range=(past_12_month_first_day,current_month_first_day)).aggregate(Sum('amount'))['amount']