[英]Django weighted percentage
I'm trying to calculate a weighted percentage in a Django query.我正在尝试计算 Django 查询中的加权百分比。
This is an example of what my data looks like:这是我的数据的示例:
id start_date agency_id area_id housetype_id no_of_changed price_change_percentage total
6716 2017-08-26 11 1 1 16 -0.09 35
6717 2017-08-26 11 1 3 44 -0.11 73
6718 2017-08-26 11 1 4 7 -0.1 12
6719 2017-08-26 11 1 5 0 0 4
6720 2017-08-26 11 1 6 0 0 1
6721 2017-08-26 21 1 1 0 0 1
6722 2017-08-26 34 1 1 0 0 1
6723 2017-08-26 35 1 1 0 0 1
6724 2017-08-26 38 1 1 0 0 1
and this is my current code:这是我当前的代码:
from django.db.models import F, FloatField, ExpressionWrapper
from app.models import PriceChange
def weighted_percentage(area_id, date_range, agency_id, housetype):
data = PriceChange.objects.filter(area_id=area_id,
start_date__range=date_range,
agency_id=agency_id,
)
if housetype:
data = data.filter(housetype=housetype) \
.values('start_date') \
.annotate(price_change_total=ExpressionWrapper((F('price_change_percentage') * F('no_of_changed')) / F('total'), output_field=FloatField())) \
.order_by('start_date')
else:
# what to do?
pass
x = [x['start_date'] for x in data]
y = [y['price_change_total'] for y in data]
return x, y
I figured out how to do the calculation when housetype is defined and I only need to data from one row.我想出了如何在定义房屋类型时进行计算并且我只需要从一行中获取数据。 I can't figure out how to done when I need to calculate for multiple rows with the same start_date.
当我需要计算具有相同 start_date 的多行时,我不知道该怎么做。 I don't want a value for each row but for each start_date.
我不想要每一行的值,而是每个 start_date 的值。
As an example (two rows with same start_date, area_id, agency_id but different housetype_ids): no_of_changed price_change_percentage total 16 -0.09 35 44 -0.11 73例如(两行具有相同的 start_date、area_id、agency_id 但不同的 housetype_id): no_of_changed price_change_percentage total 16 -0.09 35 44 -0.11 73
The calculation is in pseudocode: ((no_of_changed[0] * price_changed_percentage[0]) + (no_of_changed[0] * price_changed_percentage[0])) / (total[0] + total[1]) = price_change_total
计算采用伪代码:
((no_of_changed[0] * price_changed_percentage[0]) + (no_of_changed[0] * price_changed_percentage[0])) / (total[0] + total[1]) = price_change_total
((16 * -0.09) + (44 * -0.11)/ (35 + 73) = -0.03148148
I'm using Django 1.11 and Python 3.6.我正在使用 Django 1.11 和 Python 3.6。
You need to wrap the expression in a Sum expression.您需要将表达式包装在 Sum 表达式中。
Add the following import:添加以下导入:
from django.db.models import Sum
Then add the following query然后添加以下查询
else:
data = data.values('start_date') \
.annotate(
price_change_total=ExpressionWrapper(
Sum(F('price_change_percentage') * F('no_of_changed')) / Sum(F('total')),
output_field=FloatField()
)
) \
.order_by('start_date')
What is happening here is that when you use an aggregation expression such as Sum
inside an annotate()
call, it is translated into a group_by
query in the database.这里发生的事情是,当您在
annotate()
调用中使用诸如Sum
类的聚合表达式时,它会被转换为数据库中的group_by
查询。 All columns listed in the previous values()
clause are used to create the group_by
query.前面
values()
子句中列出的所有列都用于创建group_by
查询。
See this blog post for further explanation and a breakdown of the resulting SQL query.请参阅此博客文章以获得进一步的解释和结果 SQL 查询的细分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.