四分位数的百分比分析

时间:2019-03-20 20:20:54

标签: oracle

我试图获得每个四分位数中t3和t3之间的差异的最大值,最小值。但是,当我将百分比#更改为50.75时,我看到下面列出的输出。我不确定我在做什么错。

SELECT MIN(SPREAD),MAX(SPREAD) FROM
  (SELECT COUNT(*)*1.0 AS TOTALLINES FROM pa_fcs)A,
  (SELECT SPREAD,ROWNUM *1.0 AS LINENUM FROM (
select BOARD_RATE_T3,BOARD_RATE_T1 ,(BOARD_RATE_T3-BOARD_RATE_T1) AS SPREAD 
  from  pa_fcs ))B 
  WHERE LINENUM/TOTALLINES <= .25;

OUTPUT: .25
Min(Spread)      Max(Spread)
7.47             5160.24

Output :.50
Min(Spread)      Max(Spread)
7.47             5160.24

Output: .75
Min(Spread)      Max(Spread)
0              5160.24     

1 个答案:

答案 0 :(得分:2)

使用NTILE分析函数来计算四分位数。

然后是MINMAX琐碎的锻炼

create table tab as
select rownum spread from dual connect by level <= 100;

with ntile as (
select 
    spread,
    NTILE(4) OVER (ORDER BY spread DESC)  qtile
from tab)
select qtile, min(spread), max(spread)
from ntile
group by qtile order by 1
;

     QTILE MIN(SPREAD) MAX(SPREAD)
---------- ----------- -----------
         1          76         100
         2          51          75
         3          26          50
         4           1          25

对于您的桌子,它就像

with spr as (
  select  (BOARD_RATE_T3-BOARD_RATE_T1) AS SPREAD 
  from  pa_fcs),
ntile as (
  select 
    spread,
    NTILE(4) OVER (ORDER BY spread DESC)  qtile
  from spr)
select qtile, min(spread), max(spread)
from ntile
group by qtile order by 1