gpt4 book ai didi

mysql - SQL查询百分位数间隔,但每个间隔的行数相等?

转载 作者:行者123 更新时间:2023-11-29 05:55:43 25 4
gpt4 key购买 nike

我想为每一行分配一个索引/组编号,以指示它属于哪个百分位数。例如,我现在的代码是:

SELECT User, Revenue,
(int)(100*(PERCENT_RANK() OVER(ORDER BY Revenue)))/5 AS Percentile
FROM RevenueDistribution;

所以一个Percentile 0 表示 0-5%,1 表示 5-10%,等等。但是,使用等距间隔意味着行数不均匀,因为第一个间隔的行数比其他间隔多很多,因为有很多行带有 Revenue=0。在数据中。例如,可能有超过一百万的用户使用 Percentile=0 ,然后每个人都有几十万用户 Percentile=1,2,... .

有没有办法像这样分配一个索引,但每个间隔的行数相等,而不是间隔相等但行数不相等?

最佳答案

根据您的描述,我认为您正在搜索 NTILE :

NTILE(N) over_clause

Divides a partition into N groups (buckets), assigns each row in the partition its bucket number, and returns the bucket number of the current row within its partition. For example, if N is 4, NTILE() divides rows into quartiles. If N is 100, NTILE() divides rows into percentiles.

SELECT User, Revenue,
NTILE(100) OVER(ORDER BY Revenue) AS bucket_num
FROM RevenueDistribution;

关于mysql - SQL查询百分位数间隔,但每个间隔的行数相等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49615700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com