如何在单个Teradata查询中输出不同的第25,第50,第75百分位?

时间:2017-01-17 17:55:18

标签: sql teradata ranking percentile quartile

我几个小时后就被困在类似的东西上,并制定了一个不那么混乱的代码,用于在单个Teradata查询中输出第25,50,75个百分位数。可以进一步扩展以产生“ 5点摘要”。根据人口估计值得出最小和最大变化静态值。

有人要求优雅的方法。分享我的。

以下是代码:

SELECT MAX(PER_MIN) AS PER_MIN,
       MAX(PER_25) AS PER_25,
       MAX(PER_50)  AS PER_50,
       MAX(PER_75)  AS PER_75,
       MAX(PER_MAX) AS PER_MAX
FROM (SELECT CASE WHEN ROW_NUMBER() OVER(ORDER BY DURATION_MACRO_CURR ASC) = CAST(COUNT(*) OVER() * 0.01 AS INT) THEN DURATION_MACRO_CURR END AS PER_MIN,
             CASE WHEN ROW_NUMBER() OVER(ORDER BY DURATION_MACRO_CURR ASC) = CAST(COUNT(*) OVER() * 0.25 AS INT) THEN DURATION_MACRO_CURR END AS PER_25,
             CASE WHEN ROW_NUMBER() OVER(ORDER BY DURATION_MACRO_CURR ASC) = CAST(COUNT(*) OVER() * 0.50 AS INT) THEN DURATION_MACRO_CURR END AS PER_50
             CASE WHEN ROW_NUMBER() OVER(ORDER BY DURATION_MACRO_CURR ASC) = CAST(COUNT(*) OVER() * 0.75 AS INT) THEN DURATION_MACRO_CURR END AS PER_75
             CASE WHEN ROW_NUMBER() OVER(ORDER BY DURATION_MACRO_CURR ASC) = CAST(COUNT(*) OVER() * 0.99 AS INT) THEN DURATION_MACRO_CURR END AS PER_MAX
      FROM PROD_EXP_DL_CVM.PROD_CVM
      WHERE PW_END_DATE =  '2016-10-18'
    ) BASE

这是所需的输出:

enter image description here

1 个答案:

答案 0 :(得分:5)

我会使用条件聚合来做到这一点:

f_name