从每日百分比值计算每月百分比值

时间:2018-10-23 07:58:13

标签: sql postgresql aggregate percentile

我在postgres数据库中有一个具有以下表示形式的表:

date         name    percentile95
2018-09-01    a           0.34
2018-09-02    a           0.41
....
2018-09-29    a           0.74
2018-09-30    a           0.39
2018-10-01    a           0.31
2018-10-02    a           0.24
....
2018-10-30    a           0.64
2018-09-31    a           0.89

我每天都有对应于特定名称'a'的percentile95值。我要计算的是这些每日值中的蒙特利百分数值,所以结果看起来像这样:

date         name    percentile95
2018-09-01    a    {aggreate_percentile from sept}
2018-10-01    a    {aggreate_percentile from oct}

在SQL中有什么方法可以做到吗?

编辑:根据此link,无法计算总百分位数。那么,首先,是否有可能从每日百分位数计算出每月百分位数?

2 个答案:

答案 0 :(得分:1)

您可以尝试使用date_part函数来获取年份和月份,然后在子查询中执行SUM,然后在月份和年份上进行自联接并进行一些计算

CREATE TABLE T(
   date timestamp,
   name varchar(50),
   percentile95 float
);

insert into T values ('2018-09-01','a',0.34);
insert into T values ('2018-09-02','a',0.41);
insert into T values ('2018-09-29','a',0.74);
insert into T values ('2018-09-30','a',0.39);
insert into T values ('2018-10-01','a',0.31);
insert into T values ('2018-10-02','a',0.24);
insert into T values ('2018-10-30','a',0.64);
insert into T values ('2018-09-30','a',0.89);

查询1

select 
    date,
    t1.name, 
    concat((percentile95 * 100/total ),'%') percentile95
from T t1 JOIN (
  SELECT  
      date_part('year', date) y,
      date_part('month', date) m,
      name,
      sum(percentile95) total
  FROM T 
  group by date_part('year', date),
           date_part('month', date),
           name 
) v 
ON
  v.y = date_part('year', t1.date)
and
  v.m = date_part('month', t1.date)
and
  v.name = t1.name

Results

|                 date | name |         percentile95 |
|----------------------|------|----------------------|
| 2018-09-01T00:00:00Z |    a | 12.2743682310469318% |
| 2018-09-02T00:00:00Z |    a | 14.8014440433213004% |
| 2018-09-29T00:00:00Z |    a | 26.7148014440433208% |
| 2018-09-30T00:00:00Z |    a | 14.0794223826714795% |
| 2018-09-30T00:00:00Z |    a | 32.1299638989169694% |
| 2018-10-02T00:00:00Z |    a | 20.1680672268907557% |
| 2018-10-30T00:00:00Z |    a | 53.7815126050420176% |
| 2018-10-01T00:00:00Z |    a | 26.0504201680672267% |

答案 1 :(得分:0)

demo: db<>fiddle

使用window functionsdate_trunc

查询

SELECT
    *, 
    percentile95 / SUM(percentile95) OVER (PARTITION BY date_trunc('month', "date")) 
        * 100 as percentile_in_month
FROM
    t

结果:

date                  name   percentile95   percentile_in_month
2018-09-01 00:00:00   a      0.34           12.2743682310469
2018-09-02 00:00:00   a      0.41           14.8014440433213
2018-09-29 00:00:00   a      0.74           26.7148014440433
2018-09-30 00:00:00   a      0.39           14.0794223826715
2018-09-30 00:00:00   a      0.89           32.129963898917
2018-10-02 00:00:00   a      0.24           20.1680672268908
2018-10-30 00:00:00   a      0.64           53.781512605042
2018-10-01 00:00:00   a      0.31           26.0504201680672
  1. date_trunc将您的日期归一化为月份。
  2. 窗口函数将值分组(此处是截断的日期)
  3. 聚合函数SUM汇总窗口组中的所有值
  4. 将汇总值和原始值用于计算百分比值