首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >计算aws雅典娜中的百分位数

计算aws雅典娜中的百分位数
EN

Stack Overflow用户
提问于 2020-12-10 00:11:32
回答 2查看 7.1K关注 0票数 1

我的查询结果正在aws quicksight中使用。尽管quicksight为我们提供了percentileCont(),但我希望在查询中使用它,而不是使用计算字段。

最后,我要做的是创建一个点列

代码语言:javascript
运行
复制
under 25percentile -> 0
under 50 percentile -> 1 
under 75 percentile -> 2
rest -> 3

取决于从a,b范围内的列。

现在,我在每个百分位数中找到值,并手动创建一个范围。

代码语言:javascript
运行
复制
With table as (
    SELECT *
         , cast(date_diff('day', last_transaction, current_date) as double) as col
)
SELECT *
     , case 
         when col between 0 and 25 then 0
         when col between 26 and 66 then 1
         when col between 67 and 193 then 2
         when col >= 194 then 3
       end as point
  FROM table;

但是,我希望它是动态的,所以不是0,25,应该是min(col),25百分位数(Col)。

以上查询输出

代码语言:javascript
运行
复制
col   point
333     3
166     2
 96     1
 .

加上NTILE(),感谢@Gordon Linoff

代码语言:javascript
运行
复制
With table as (
    SELECT *
         , cast(date_diff('day', last_transaction, current_date) as double) as col
)
SELECT *
     , case 
         when col between 0 and 25 then 0
         when col between 26 and 66 then 1
         when col between 67 and 193 then 2
         when col >= 194 then 3
       end as point
      , NTILE(4) over(order by col) as pt
  FROM table;

输出

代码语言:javascript
运行
复制
col   point
0     1
0     1
0     1
 .

似乎把计算搞砸了

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-12-10 00:36:10

您所描述的几乎是ntile()函数:

代码语言:javascript
运行
复制
SELECT t.*,,
       NTILE(4) OVER (ORDER BY col) - 1 as point
FROM table;

两个注意事项:

  • NTILE(<n>)返回1与n
  • NTILE()之间的值,以确保结果块相等。这意味着边界上的值可能会在不同的回收箱中结束。

将值放在单独的回收箱中(但回收箱可能有不同的大小)的另一种选择是percent_rank()。就你而言:

代码语言:javascript
运行
复制
SELECT t.*,,
       CEILING(PRECENT_RANK() OVER (ORDER BY col) * 4) - 1 as point
FROM table;
票数 5
EN

Stack Overflow用户

发布于 2020-12-10 00:14:02

在Presto中,我认为approx_percentile()case表达式可以做您想做的事情:

代码语言:javascript
运行
复制
select t.*, 
    case
        when col <= approx_percentile(col, 0.25) over() then 0
        when col <= approx_percentile(col, 0.50) over() then 1
        when col <= approx_percentile(col, 0.75) over() then 2
        else 3
    end as point
from mytable t
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65226749

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档