<tfoot id='YgibS'></tfoot>

        <small id='YgibS'></small><noframes id='YgibS'>

        <legend id='YgibS'><style id='YgibS'><dir id='YgibS'><q id='YgibS'></q></dir></style></legend>
        <i id='YgibS'><tr id='YgibS'><dt id='YgibS'><q id='YgibS'><span id='YgibS'><b id='YgibS'><form id='YgibS'><ins id='YgibS'></ins><ul id='YgibS'></ul><sub id='YgibS'></sub></form><legend id='YgibS'></legend><bdo id='YgibS'><pre id='YgibS'><center id='YgibS'></center></pre></bdo></b><th id='YgibS'></th></span></q></dt></tr></i><div id='YgibS'><tfoot id='YgibS'></tfoot><dl id='YgibS'><fieldset id='YgibS'></fieldset></dl></div>
          <bdo id='YgibS'></bdo><ul id='YgibS'></ul>

        姜戈Postgres - 百分位数(中位数)和分组依据

        Django amp; Postgres - percentile (median) and group by(姜戈Postgres - 百分位数(中位数)和分组依据)
          <bdo id='P9yRN'></bdo><ul id='P9yRN'></ul>
          1. <small id='P9yRN'></small><noframes id='P9yRN'>

            <legend id='P9yRN'><style id='P9yRN'><dir id='P9yRN'><q id='P9yRN'></q></dir></style></legend>
              <i id='P9yRN'><tr id='P9yRN'><dt id='P9yRN'><q id='P9yRN'><span id='P9yRN'><b id='P9yRN'><form id='P9yRN'><ins id='P9yRN'></ins><ul id='P9yRN'></ul><sub id='P9yRN'></sub></form><legend id='P9yRN'></legend><bdo id='P9yRN'><pre id='P9yRN'><center id='P9yRN'></center></pre></bdo></b><th id='P9yRN'></th></span></q></dt></tr></i><div id='P9yRN'><tfoot id='P9yRN'></tfoot><dl id='P9yRN'><fieldset id='P9yRN'></fieldset></dl></div>

            • <tfoot id='P9yRN'></tfoot>

                    <tbody id='P9yRN'></tbody>

                1. 本文介绍了姜戈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

                  推荐答案

                  您可以像 Ryan Murphy (https://gist.github.com/rdmurphy/3f73c7b1826cacee34f6c2a855b12e2e).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'] 的形式提供.

                  which is then available as my_model_aggregate['period__median'].

                  这篇关于姜戈Postgres - 百分位数(中位数)和分组依据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                  Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                  Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                  How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                  Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                  Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)
                    <bdo id='HvzFT'></bdo><ul id='HvzFT'></ul>
                        • <i id='HvzFT'><tr id='HvzFT'><dt id='HvzFT'><q id='HvzFT'><span id='HvzFT'><b id='HvzFT'><form id='HvzFT'><ins id='HvzFT'></ins><ul id='HvzFT'></ul><sub id='HvzFT'></sub></form><legend id='HvzFT'></legend><bdo id='HvzFT'><pre id='HvzFT'><center id='HvzFT'></center></pre></bdo></b><th id='HvzFT'></th></span></q></dt></tr></i><div id='HvzFT'><tfoot id='HvzFT'></tfoot><dl id='HvzFT'><fieldset id='HvzFT'></fieldset></dl></div>
                        • <legend id='HvzFT'><style id='HvzFT'><dir id='HvzFT'><q id='HvzFT'></q></dir></style></legend>
                        • <small id='HvzFT'></small><noframes id='HvzFT'>

                          <tfoot id='HvzFT'></tfoot>
                            <tbody id='HvzFT'></tbody>