ACCESS–分组排名获取常驻小区
- 2019 年 12 月 26 日
- 笔记
现如今,端到端感知优化越来越重要了,有时,我们需要获取每个用户的 TOPN
常驻小区。 假设我们有如下业务报表:

我们要获取每个用户的 TOP3
常驻小区,少量小区用 excel
处理就好了,但是一般这种用户级报表都是几百万行,这个时候,数据库就该上场了。
我们先给这个报表添加一列分组排名来标记每个用户不同占用小区的流量,然后筛选分组排名为1,2,3
就好了,今天看一下 ACCESS
如何实现。
1)首先对原表进行排序;
SELECT user.msisdn, user.cellname, user.throughput FROM [user] ORDER BY user.msisdn, user.throughput DESC;
得到如下结果:

2)该表基础上,建立子查询得到分组排名;
SELECT qry1.msisdn, qry1.cellname, qry1.throughput, (select count(*) from qry1 as tbl2 where qry1.throughput < tbl2.throughput and qry1.msisdn = tbl2.msisdn)+1 AS `rankN` FROM qry1;
得到如下结果:

3)将分组排名限定为 1,2,3
,得到最终答案;
SELECT qry1.msisdn, qry1.cellname, qry1.throughput, (select count(*) from qry1 as tbl2 where qry1.throughput < tbl2.throughput and qry1.msisdn = tbl2.msisdn)+1 AS rankN FROM qry1 WHERE ((((select count(*) from qry1 as tbl2 where qry1.throughput < tbl2.throughput and qry1.msisdn = tbl2.msisdn)+1) In (1,2,3)));

回复 user
获取完整文件