具有基数的 Postgres percentile_cont
Postgres percentile_cont with cardinality
自发布以来,我一直在使用 Postgres 中的新 percentile_cont 来计算 table 的百分位数。但是,我们现在正在更改 table 以包括每一行的基数,我不确定如何实现 percentile_cont 以考虑到这一点。
假设 table 之前看起来像这样:
+--------+--------------+
| name | age |
+--------+--------------+
| Joe | 10 |
+--------+--------------+
| Bob | 11 |
+--------+--------------+
| Lisa | 12 |
+--------+--------------+
计算集合中年龄的第 85 个百分位数只需使用:percentile_cont(0.85) WITHIN group (ORDER BY age asc) 85
现在,我们有每个名字的基数(具有该特定名字的人数)。它看起来像这样:
+--------------+--------+
| name | age | count |
+--------+-----+--------+
| Joe | 10 | 2 |
+--------+-----+--------+
| Bob | 11 | 1 |
+--------+-----+--------+
| Lisa | 12 | 1 |
+--------+-----+--------+
有没有什么方法可以使用 percentile_cont 或 Postgres 中的任何其他内置函数来计算考虑 count/cardinality 的百分位数?
最明显的解决方案是根据count
复制行。
示例数据:
create table a_table (name text, age int, count int);
insert into a_table values
('Joe', 10, 3),
('Bob', 11, 2),
('Lisa', 12, 1);
查询:
with recursive data (name, age, count) as (
select *
from a_table
union all
select name, age, count- 1
from data
where count > 1
)
select name, age
from data
order by 1, 2;
name | age
------+-----
Bob | 11
Bob | 11
Joe | 10
Joe | 10
Joe | 10
Lisa | 12
(6 rows)
自发布以来,我一直在使用 Postgres 中的新 percentile_cont 来计算 table 的百分位数。但是,我们现在正在更改 table 以包括每一行的基数,我不确定如何实现 percentile_cont 以考虑到这一点。
假设 table 之前看起来像这样:
+--------+--------------+
| name | age |
+--------+--------------+
| Joe | 10 |
+--------+--------------+
| Bob | 11 |
+--------+--------------+
| Lisa | 12 |
+--------+--------------+
计算集合中年龄的第 85 个百分位数只需使用:percentile_cont(0.85) WITHIN group (ORDER BY age asc) 85
现在,我们有每个名字的基数(具有该特定名字的人数)。它看起来像这样:
+--------------+--------+
| name | age | count |
+--------+-----+--------+
| Joe | 10 | 2 |
+--------+-----+--------+
| Bob | 11 | 1 |
+--------+-----+--------+
| Lisa | 12 | 1 |
+--------+-----+--------+
有没有什么方法可以使用 percentile_cont 或 Postgres 中的任何其他内置函数来计算考虑 count/cardinality 的百分位数?
最明显的解决方案是根据count
复制行。
示例数据:
create table a_table (name text, age int, count int);
insert into a_table values
('Joe', 10, 3),
('Bob', 11, 2),
('Lisa', 12, 1);
查询:
with recursive data (name, age, count) as (
select *
from a_table
union all
select name, age, count- 1
from data
where count > 1
)
select name, age
from data
order by 1, 2;
name | age
------+-----
Bob | 11
Bob | 11
Joe | 10
Joe | 10
Joe | 10
Lisa | 12
(6 rows)