我想要使用django orm
获取最近的avg数据select product_name, AVG(price) as avg_price
from (
select procuct_name, price, deal_date
from deal_history
order by deal_date desc
) R
所以,我试过这个orm
DealHistory.objects.all().order_by('-deal_date')\
.values('product_name').annotate(avg_price=Avg('price'))
但是这个django-orm返回SQL查询
select product_name, deal_date, avg(price) as avg_price
from deal_history
order by deal_date desc
group by product_name, deal_date
我该怎么办?
答案 0 :(得分:0)
如果你想拥有所有记录的平均值,你必须使用聚合。为每条记录单独注释。