在SQL中计算百分位数

时间:2018-07-26 15:47:34

标签: sql oracle percentile quartile

这应该非常简单,但是作为SQL的新手,我真的很努力。建议我对连续(非离散)数据使用PERCENTILE_CONT。

有问题的数据涉及两列:(1)患者列表的ID;(2)每年平均事件数。

使用我在网上找到的一些代码,这是我的工作

SELECT ID,
percentile_cont (0.25) WITHIN GROUP
(ORDER BY PPPY ASC) OVER(PARTITION BY ID) as percentile_25,
percentile_cont (0.50) WITHIN GROUP
(ORDER BY PPPY ASC) OVER(PARTITION BY ID) as percentile_50,
percentile_cont (0.75) WITHIN GROUP
(ORDER BY PPPY ASC) OVER(PARTITION BY ID) as percentile_75
FROM AE_COUNT;

这似乎只是向您报告每列具有相同PPPY值的列。

知道我要去哪里哪里吗?

2 个答案:

答案 0 :(得分:0)

假设您要获取整个表格的百分位数,请尝试以下操作:

SELECT Distinct
percentile_cont (0.25) WITHIN GROUP
(ORDER BY PPPY ASC) OVER() as percentile_25,
percentile_cont (0.50) WITHIN GROUP
(ORDER BY PPPY ASC) OVER() as percentile_50,
percentile_cont (0.75) WITHIN GROUP
(ORDER BY PPPY ASC) OVER() as percentile_75
FROM AE_COUNT;

删除分区语句将对整个表运行它。我还从选择语句中删除了ID列,并将其与众不同。

我还要指出,您说的第二列是每年平均个事件。我不知道您需要什么百分位数,但是要知道,计算一组集合的平均值的百分位数不会产生与计算集合并集的百分位数相同的结果。

答案 1 :(得分:0)

PERCENTILE_CONT()是窗口函数或集合函数。如果要为所有数据汇总一行,请将其用作聚合函数:

SELECT percentile_cont(0.25) WITHIN GROUP (ORDER BY PPPY ASC)  as percentile_25,
       percentile_cont(0.50) WITHIN GROUP (ORDER BY PPPY ASC) as percentile_50,
       percentile_cont(0.75) WITHIN GROUP (ORDER BY PPPY ASC) as percentile_75
FROM AE_COUNT;

如果想要每个患者的价值,您可以这样做:

SELECT id, percentile_cont(0.25) WITHIN GROUP (ORDER BY PPPY ASC)  as percentile_25,
       percentile_cont(0.50) WITHIN GROUP (ORDER BY PPPY ASC) as percentile_50,
       percentile_cont(0.75) WITHIN GROUP (ORDER BY PPPY ASC) as percentile_75
FROM AE_COUNT
GROUP BY id;

但是,患者的行数可能很少,因此任何给定患者的值都可能相同。