我需要从一些数据生成特定的报告,并且在弄清楚 PERCENTILE_CONT 的正确用法以提供我需要的结果时遇到了很大的麻烦。我想在查询结果中包含一列,该列显示一系列值中第 95 个百分位的值。
我有一个表如下:
customer_id sale_amount sale_date
1 265.75 2019-09-11 00:00:04.000
1 45.75 2019-09-10 01:00:04.000
1 2124.77 2019-09-10 04:00:04.000
1 66.99 2019-09-10 04:20:04.000
1 266.49 2019-09-09 11:20:04.000
1 3266.49 2019-09-08 11:20:04.000
Run Code Online (Sandbox Code Playgroud)
非常简单。
我可以运行以下查询,没有问题:
select
min(sale_amount) as minimum_sale,
max(sale_amount) as maximum_sale,
avg(sale_amount) as average_sale
from
sales
where
customer_id = 1;
Run Code Online (Sandbox Code Playgroud)
这会产生以下输出:
minimum_sale maximum_sale average_sale
45.75 3266.49 1006.040000
Run Code Online (Sandbox Code Playgroud)
我想要的是第四列 perc_95,它将计算代表 sale_amount 的第 95 个百分位数的值。
这可以让我获得价值:
select distinct
customer_id,
percentile_cont(0.95) WITHIN GROUP (order by sale_amount) OVER (partition by customer_id) as perc_95 …Run Code Online (Sandbox Code Playgroud) 我目前有一个查询,其工作方式如下:
select AVG(t2 - t1) as delay,
percentile_cont(0.25) within group (order by (t2 - t1)) as q25,
percentile_cont(0.5) within group (order by (t2 - t1)) as median,
percentile_cont(0.75) within group (order by (t2 - t1)) as q75,
p.bool1,
p.cat1
from people p
group by p.bool1, p.cat1
order by p.cat1,p.bool1
Run Code Online (Sandbox Code Playgroud)
但是,我在 postgres 函数聚合页面上阅读: https://www.postgresql.org/docs/9.4/functions-aggregate.html
我应该能够指定多个分位数:
percentile_cont(fractions) WITHIN GROUP (ORDER BY sort_expression) double precision[] double precision or interval array of sort expression's type multiple continuous percentile: returns an array of results matching …Run Code Online (Sandbox Code Playgroud)