【问题标题】:How to calculate the median in Postgres?如何计算 Postgres 中的中位数?
【发布时间】:2020-09-28 19:39:12
【问题描述】:

我已经创建了一个基本数据库(附图片)Database,我正在尝试查找以下内容:

“每个日历月每位用户花费的总金额中位数”

我尝试了以下方法,但出现错误:

SELECT 
user_id,
AVG(total_per_user)
FROM (SELECT user_id,
        ROW_NUMBER() over (ORDER BY total_per_user DESC) AS desc_total,
        ROW_NUMBER() over (ORDER BY total_per_user ASC) AS asc_total
      FROM (SELECT EXTRACT(MONTH FROM created_at) AS calendar_month,
            user_id,    
            SUM(amount) AS total_per_user
            FROM transactions
            GROUP BY calendar_month, user_id) AS total_amount   
      ORDER BY user_id) AS a
WHERE asc_total IN (desc_total, desc_total+1, desc_total-1)
GROUP BY user_id
;

【问题讨论】:

  • 什么错误?如果您不告诉我们问题,我们将无法帮助您。

标签: sql postgresql date group-by median


【解决方案1】:

只需使用percentile_cont()。我不完全理解这个问题。如果你想要每月支出的中位数,那么:

SELECT user_id,
       PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY total_per_user
        ROW_NUMBER() over (ORDER BY total_per_user DESC) AS desc_total,
        ROW_NUMBER() over (ORDER BY total_per_user ASC) AS asc_total
FROM (SELECT DATE_TRUNC('month', created_at) AS calendar_month,
             user_id, SUM(amount) AS total_per_user
      FROM transactions t
      GROUP BY calendar_month, user_id
     ) um   
GROUP BY user_id;

中位数有一个内置函数。无需花哨的处理。

【讨论】:

    【解决方案2】:

    在 Postgres 中,您可以使用 aggregate function percentile_cont():

    select 
        user_id,
        percentile_cont(0.5) within group(order by total_per_user) median_total_per_user
    from (
        select user_id, sum(amount) total_per_user
        from transactions
        group by date_trunc('month', created_at), user_id
    ) t
    group by user_id
    

    请注意,date_trunc() 可能比 extract(month from ...) 更接近您想要的 - 除非您确实想将不同年份的同一月份的金额相加,这不是我理解您的要求的方式。

    【讨论】:

      猜你喜欢
      • 2023-04-08
      • 2015-03-09
      • 1970-01-01
      • 2021-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-01
      • 1970-01-01
      相关资源
      最近更新 更多