首页 > 解决方案 > 如何使用 Oracle 的 Toad 11g (SQL) 将一组实体(人)分成 10 个相等的部分,但基于它们对 EX 的使用

问题描述

嗨,我有一份超过 2 万人的名单,他们的使用情况从大到小排列。

我尝试使用 row_number () over (partition by user column order by usage desc) as rnk

但这没有用..结果很疯狂。

简单地说,我只想要 10 个相等的 10 组,第一组由我首先列出的顺序中使用率最高的组组成。

帮助!

标签: sql

解决方案


You can use ntile():

select t.*, ntile(10) over (order by usage desc) as usage_decile
from t;

The only caveat: This will divide the data into exactly 10 equal sized groups. If usage values have duplicates, then users with the same usage will be in different deciles.

If you don't want that behavior, use a more manual calculation:

select t.*,
       ceil(rank() over (order by usage desc) * 10 /
            count(*) over ()
           ) as usage_decile
from t;

推荐阅读