计算每分位数的元素数

时间:2017-04-18 23:34:31

标签: mysql

我有一张类似这样的表:

Name | Frequency
----------------
Bill |  12
Joe  |  23
Hank |  1
Stew |  98

我需要弄清楚有多少人构成每个十分位数的总频率。即如果总sum(frequency)为10,000,则每个十分位数的大小为1,000。我需要知道每1000人中有多少人。现在我已经完成了:

with rankedTable as (select * from TABLE order by frequency desc limit XXXX)
select sum(frequency) from rankedTable

我正在更改XXXX,以便sum(frequency)加起来为十分位数值(我从sum(frequency)/10知道)。必须有一种更快的方法。

1 个答案:

答案 0 :(得分:0)

我认为这可以给出用户所属的n百分位数。我使用变量来提高可读性,但它们并不是绝对必要的。

set @sum := (select sum(freq) from t);
set @n := 10; -- define the N in N-perentile

select b.name, b.freq, sum(a.freq) as cumulative_sum, floor(sum(a.freq) / @sum * @n) as percentile
from t a join t b on b.freq >= a.freq
group by b.name

从中可以很容易地计算每个百分位数的成员:

select percentile, count(*) as `count`
from 
(
    select b.name, b.freq, sum(a.freq) as cumulative_sum, floor(sum(a.freq) / @sum * @n) as percentile
    from t a join t b on b.freq >= a.freq
    group by b.name
) x
group by percentile;

我希望这有帮助!