Django amp; Postgres - percentile (median) and group by(姜戈amp;Postgres - 百分位数(中位数)和分组依据)
问题描述
我需要计算每个卖家 ID 的周期中位数(参见下面的简化模型).问题是我无法构建 ORM 查询.
I need to calculate period medians per seller ID (see simplyfied model below). The problem is I am unable to construct the ORM query.
型号
class MyModel:
    period = models.IntegerField(null=True, default=None)
    seller_ids = ArrayField(models.IntegerField(), default=list)
    aux = JSONField(default=dict)
查询
queryset = (
    MyModel.objects.filter(period=25)
    .annotate(seller_id=Func(F("seller_ids"), function="unnest"))
    .values("seller_id")
    .annotate(
        duration=Cast(KeyTextTransform("duration", "aux"), IntegerField()),
        median=Func(
            F("duration"),
            function="percentile_cont",
            template="%(function)s(0.5) WITHIN GROUP (ORDER BY %(expressions)s)",
        ),
    )
    .values("median", "seller_id")
)
ArrayField 聚合 (seller_id) 来源
我认为我需要做的是沿着下面的路线
I think what I need to do is something along the lines below
select t.*, p_25, p_75
from t join
     (select district,
             percentile_cont(0.25) within group (order by sales) as p_25,
             percentile_cont(0.75) within group (order by sales) as p_75
      from t
      group by district
     ) td
     on t.district = td.district
以上示例源
Python 3.7.5、Django 2.2.8、Postgres 11.1
Python 3.7.5, Django 2.2.8, Postgres 11.1
推荐答案
您可以创建 Aggregate 类的 Median 子类,就像 Ryan Murphy (https://gist.github.com/rdmurphy/3f73c7b1826cacee34f6c2a82e2e2e).Median 然后就像 Avg 一样工作:
You can create a Median child class of the Aggregate class as was done by Ryan Murphy (https://gist.github.com/rdmurphy/3f73c7b1826cacee34f6c2a855b12e2e). Median then works just like Avg:
    from django.db.models import Aggregate, FloatField
    class Median(Aggregate):
        function = 'PERCENTILE_CONT'
        name = 'median'
        output_field = FloatField()
        template = '%(function)s(0.5) WITHIN GROUP (ORDER BY %(expressions)s)'
然后找到一个字段的中位数使用
Then to find the median of a field use
    my_model_aggregate = MyModel.objects.all().aggregate(Median('period'))
然后可用作 my_model_aggregate['period__median'].
这篇关于姜戈&Postgres - 百分位数(中位数)和分组依据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:姜戈&Postgres - 百分位数(中位数)和分组依据
				
        
 
            
        基础教程推荐
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
 - Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
 - 求两个直方图的卷积 2022-01-01
 - 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
 - PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
 - 包装空间模型 2022-01-01
 - 修改列表中的数据帧不起作用 2022-01-01
 - PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
 - 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
 - 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
				
				
				
				