1

有没有一种简单的方法来编写 SQL 四分位数函数?

假设我有一个列表 - 表中的列值 (0,0,0,1,1,4,5,7,8,25,100,100,101,260,360,370}

生成结果集 a-la 的好方法是什么:

select 'Q1', quartile(1,col_name) from table_name
union
select 'Q2', quartile(2,col_name) from table_name
union
select 'Q3', quartile(3,col_name) from table_name
union
select 'Q4', quartile(4,col_name) from table_name
;

到目前为止,我还没有发现任何可用于这方面的东西。

4

1 回答 1

3

我不明白你为什么想要 Q4 :) 不需要为此使用 percentile_cont() 函数,你可以使用 max() 来获得它。

此代码可能会对您有所帮助。

select      'q1' as q
            , percentile_cont(0.25) within group (order by col) as q_val
from        table
union
select      'q2' as q
            , percentile_cont(0.5) within group (order by col) as q_val
from        table
union
select      'q3' as q
            , percentile_cont(0.75) within group (order by col) as q_val
from        table;
于 2016-06-10T10:39:48.833 回答