sql - 如何使用 Oracle 的 Toad 11g (SQL) 将一组实体(人)分成 10 个相等的部分,但基于它们对 EX 的使用
问题描述
嗨,我有一份超过 2 万人的名单,他们的使用情况从大到小排列。
我尝试使用 row_number () over (partition by user column order by usage desc) as rnk
但这没有用..结果很疯狂。
简单地说,我只想要 10 个相等的 10 组,第一组由我首先列出的顺序中使用率最高的组组成。
帮助!
解决方案
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;
推荐阅读
- laravel - 背景图像在 foreach 循环中不起作用
- r - Removing tick marks, tick=FALSE do not seem to work
- javascript - Magento - 前端和后端没有 js 文件
- c# - 如何在 .net 核心中添加 ws-security 标头?
- angular - Angular - 如何使用自定义输入组件子组件测试容器组件?
- llvm - Enable a llvm pass for Os but not O2
- java - 我如何在java中发送带有“soap”的文件
- php - 我怎样才能保存我的价值使用 laravel 5.7 代码到 MySQL 数据库?
- azure-devops - 如何将参数添加到任务组
- php - 变量中的随机数不重复