sql - 选择 1% 的不同值
问题描述
SQL Server 2008 R2
我有一个名为 Actions 的表,这将是它的一个片段
ActionID | ActionType | ActionUserID | ActionDateTime
---------+------------+--------------+---------------------
555363 Open 9843 2020-09-15 09:27:55
555364 Process 2563 2020-09-15 09:31:22
555365 Close 8522 2020-09-15 09:37:48
555366 Detour 9843 2020-09-15 09:42:42
555367 Process 9843 2020-09-15 09:51:50
555368 Close 8522 2020-09-15 09:55:45
555369 Open 1685 2020-09-15 09:57:12
555370 Detour 2563 2020-09-15 10:03:23
555371 Detour 9843 2020-09-15 10:04:33
555372 Close 8522 2020-09-15 10:07:44
该表有数十万行。我想要做的是查看每个用户在特定月份执行的所有操作的 1%。
我知道我可以通过以下方式获得 1% 的收益:
SELECT TOP 1 PERCENT *
FROM Actions
WHERE ActionDateTime BETWEEN '09/01/2020' AND '09/30/2020'
ORDER BY NEWID()
我知道我可以通过以下方式获得特定用户的 1%:
SELECT TOP 1 PERCENT *
FROM Actions
WHERE ActionUserID = 9843
AND ActionDateTime BETWEEN '09/01/2020' AND '09/30/2020'
ORDER BY NEWID()
但我真正想要得到的是每个用户的 1%。我知道我可以通过以下方式获得当月执行操作的用户列表:
SELECT DISTINCT(ActionUserID)
WHERE ActionDateTime BETWEEN '09/01/2020' AND '09/30/2020'
但是我不确定如何组合这两个查询。
解决方案
但我真正想要得到的是每个用户的 1%。
我会推荐窗口功能percent_rank()
:
select *
from (
select a.*, percent_rank() over(partition by actionuserid order by newid()) prn
from actions a
where actiondatetime >= '20200901' and actiondatetime < '20201001'
) a
where prn < 0.01
如果您的 SQL Server 版本太旧以至于不支持percent_rank()
,那么我们可以使用rank()
and来模拟它count()
:
select *
from (
select a.*,
rank() over(partition by actionuserid order by newid()) as rn,
count(*) over(partition by actionuserid) as cnt
from actions a
where actiondatetime >= '20200901' and actiondatetime < '20201001'
) a
where 100.0 * rn / cnt < 1 or (rn = 1 and cnt < 100)
推荐阅读
- data-structures - 更新给定索引处的值,并在同一查询中找到第 K 个最小的元素
- css - 不确定时如何更改AngularJS Material md-checkbox background-image?
- javascript - 如何比较两个百分比字符串
- json - pandas.to_json - 停止将值转换为字符串
- node.js - 下载文件以响应对 Express 的 POST 请求
- amazon-web-services - 在 DynamoDB 中查询主表与扫描二级索引
- javascript - 角度自定义输入掩码
- html - 溢出文本块直接跳转到 Angular 中的交互元素
- css - 如何在不使用媒体查询的情况下根据屏幕大小使 CSS Grid 填充所有可用空间
- python - Python如何搜索关键字列表并输出匹配的列数