我的查询结果正在aws quicksight中使用。尽管quicksight为我们提供了percentileCont(),但我希望在查询中使用它,而不是使用计算字段。
最后,我要做的是创建一个点列
under 25percentile -> 0
under 50 percentile -> 1
under 75 percentile -> 2
rest -> 3
取决于从a,b范围内的列。
现在,我在每个百分位数中找到值,并手动创建一个范围。
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)。
以上查询输出
col point
333 3
166 2
96 1
.
加上NTILE(),感谢@Gordon Linoff
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;
输出
col point
0 1
0 1
0 1
.
似乎把计算搞砸了
发布于 2020-12-10 00:36:10
您所描述的几乎是ntile()
函数:
SELECT t.*,,
NTILE(4) OVER (ORDER BY col) - 1 as point
FROM table;
两个注意事项:
NTILE(<n>)
返回1与nNTILE()
之间的值,以确保结果块相等。这意味着边界上的值可能会在不同的回收箱中结束。将值放在单独的回收箱中(但回收箱可能有不同的大小)的另一种选择是percent_rank()
。就你而言:
SELECT t.*,,
CEILING(PRECENT_RANK() OVER (ORDER BY col) * 4) - 1 as point
FROM table;
发布于 2020-12-10 00:14:02
在Presto中,我认为approx_percentile()
和case
表达式可以做您想做的事情:
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
https://stackoverflow.com/questions/65226749
复制相似问题