数据库“行专列”操作---使用row_number()over(partition by 分组字段 [order by 排序字段])
测试样例:
create table test(rsrp string,rsrq string,tkey string,distan string); insert into test values('-90.28','-37','tkey1','');
insert into test values('-92.35','-40','tkey1','');
insert into test values('-94.36','-34','tkey2','');
insert into test values('-93.88','-38','tkey2',''); select * from test;
+------------+------------+------------+--------------+--+
| test.rsrp | test.rsrq | test.tkey | test.distan |
+------------+------------+------------+--------------+--+
| -90.28 | -37 | tkey1 | 10 |
| -92.35 | -40 | tkey1 | 30 |
| -94.36 | -34 | tkey2 | 5 |
| -93.88 | -38 | tkey2 | 19 |
+------------+------------+------------+--------------+--+ select rsrp,rsrq,tkey,distan,row_number()over(partition by tkey order by distan asc) rn
from test
group by rsrp,rsrq,tkey,distan;
+---------+-------+--------+---------+-----+--+
| rsrp | rsrq | tkey | distan | rn |
+---------+-------+--------+---------+-----+--+
| -90.28 | -37 | tkey1 | 10 | 1 |
| -92.35 | -40 | tkey1 | 30 | 2 |
| -93.88 | -38 | tkey2 | 19 | 1 |
| -94.36 | -34 | tkey2 | 5 | 2 |
+---------+-------+--------+---------+-----+--+
新建测试表:
create table grid_test(
buildingid nvarchar(32),
gridid nvarchar(32),
height int,
gridx nvarchar(32),
gridy nvarchar(32),
eci nvarchar(32),
total_num int
)
测试分组
insert into grid_test values('building1','gridid1',1,'gridid1 x','gridid1 y','eci1',123)
insert into grid_test values('building1','gridid1',1,'gridid1 x','gridid1 y','eci1',2)
insert into grid_test values('building1','gridid1',1,'gridid1 x','gridid1 y','eci1',19)
insert into grid_test values('building1','gridid1',1,'gridid1 x','gridid1 y','eci1',50)
insert into grid_test values('building1','gridid1',1,'gridid1 x','gridid1 y','eci1',78) insert into grid_test values('building1','gridid1',1,'gridid1 x','gridid1 y','eci2',98)
insert into grid_test values('building1','gridid1',1,'gridid1 x','gridid1 y','eci2',22)
insert into grid_test values('building1','gridid1',1,'gridid1 x','gridid1 y','eci2',19)
insert into grid_test values('building1','gridid1',1,'gridid1 x','gridid1 y','eci2',87)
insert into grid_test values('building1','gridid1',1,'gridid1 x','gridid1 y','eci2',78) --栅格小区分组排序
select buildingid,gridid,height,gridx,gridy,eci,total_num,row_number()over(partition by buildingid,gridid,height,gridx,gridy,eci order by total_num desc) rn
from grid_test
group by buildingid,gridid,height,gridx,gridy,eci,total_num; buildingid gridid height gridx gridy eci total_num rn
building1 gridid1 1 gridid1 x gridid1 y eci1 123 1
building1 gridid1 1 gridid1 x gridid1 y eci1 78 2
building1 gridid1 1 gridid1 x gridid1 y eci1 50 3
building1 gridid1 1 gridid1 x gridid1 y eci1 19 4
building1 gridid1 1 gridid1 x gridid1 y eci1 2 5
building1 gridid1 1 gridid1 x gridid1 y eci2 98 1
building1 gridid1 1 gridid1 x gridid1 y eci2 87 2
building1 gridid1 1 gridid1 x gridid1 y eci2 78 3
building1 gridid1 1 gridid1 x gridid1 y eci2 22 4
building1 gridid1 1 gridid1 x gridid1 y eci2 19 5 --栅格分组排序
select buildingid,gridid,height,gridx,gridy,eci,total_num,row_number()over(partition by buildingid,gridid,height,gridx,gridy order by total_num desc) rn
from grid_test
group by buildingid,gridid,height,gridx,gridy,eci,total_num buildingid gridid height gridx gridy eci total_num rn
building1 gridid1 1 gridid1 x gridid1 y eci1 123 1
building1 gridid1 1 gridid1 x gridid1 y eci2 98 2
building1 gridid1 1 gridid1 x gridid1 y eci2 87 3
building1 gridid1 1 gridid1 x gridid1 y eci1 78 4
building1 gridid1 1 gridid1 x gridid1 y eci2 78 5
building1 gridid1 1 gridid1 x gridid1 y eci1 50 6
building1 gridid1 1 gridid1 x gridid1 y eci2 22 7
building1 gridid1 1 gridid1 x gridid1 y eci1 19 8
building1 gridid1 1 gridid1 x gridid1 y eci2 19 9
building1 gridid1 1 gridid1 x gridid1 y eci1 2 10
行专列:
truncate table grid_test;
insert into grid_test values('building1','gridid1',1,'gridid1 x','gridid1 y','eci10',123);
insert into grid_test values('building1','gridid1',1,'gridid1 x','gridid1 y','eci11',2);
insert into grid_test values('building1','gridid1',1,'gridid1 x','gridid1 y','eci12',19);
insert into grid_test values('building1','gridid1',1,'gridid1 x','gridid1 y','eci13',50);
insert into grid_test values('building1','gridid1',1,'gridid1 x','gridid1 y','eci14',78);
insert into grid_test values('building1','gridid2',1,'gridid2 x','gridid2 y','eci21',98);
insert into grid_test values('building1','gridid2',1,'gridid2 x','gridid2 y','eci22',22);
insert into grid_test values('building1','gridid2',1,'gridid2 x','gridid2 y','eci23',19);
insert into grid_test values('building1','gridid2',1,'gridid2 x','gridid2 y','eci24',87);
insert into grid_test values('building1','gridid2',1,'gridid2 x','gridid2 y','eci25',78); select buildingid,gridid,height,gridx,gridy,eci,total_num,row_number()over(partition by buildingid,gridid,height,gridx,gridy order by total_num desc) rn
from grid_test
group by buildingid,gridid,height,gridx,gridy,eci,total_num buildingid gridid height gridx gridy eci total_num rn
building1 gridid1 1 gridid1 x gridid1 y eci10 123 1
building1 gridid1 1 gridid1 x gridid1 y eci14 78 2
building1 gridid1 1 gridid1 x gridid1 y eci13 50 3
building1 gridid1 1 gridid1 x gridid1 y eci12 19 4
building1 gridid1 1 gridid1 x gridid1 y eci11 2 5
building1 gridid2 1 gridid2 x gridid2 y eci21 98 1
building1 gridid2 1 gridid2 x gridid2 y eci24 87 2
building1 gridid2 1 gridid2 x gridid2 y eci25 78 3
building1 gridid2 1 gridid2 x gridid2 y eci22 22 4
building1 gridid2 1 gridid2 x gridid2 y eci23 19 5 select * from (
select buildingid,gridid,height,gridx,gridy,eci,total_num,row_number()over(partition by buildingid,gridid,height,gridx,gridy order by total_num desc) rn
from grid_test
group by buildingid,gridid,height,gridx,gridy,eci,total_num
) t10
where rn<=3
buildingid gridid height gridx gridy eci total_num rn
building1 gridid1 1 gridid1 x gridid1 y eci10 123 1
building1 gridid1 1 gridid1 x gridid1 y eci14 78 2
building1 gridid1 1 gridid1 x gridid1 y eci13 50 3
building1 gridid2 1 gridid2 x gridid2 y eci21 98 1
building1 gridid2 1 gridid2 x gridid2 y eci24 87 2
building1 gridid2 1 gridid2 x gridid2 y eci25 78 3 truncate table grid_test;
insert into grid_test values('building1','gridid1',1,'gridid1 x','gridid1 y','eci10',123);
insert into grid_test values('building1','gridid1',1,'gridid1 x','gridid1 y','eci11',2);
insert into grid_test values('building1','gridid1',1,'gridid1 x','gridid1 y','eci12',19);
insert into grid_test values('building1','gridid1',1,'gridid1 x','gridid1 y','eci12',19);
insert into grid_test values('building1','gridid1',1,'gridid1 x','gridid1 y','eci13',50);
insert into grid_test values('building1','gridid1',1,'gridid1 x','gridid1 y','eci14',78);
insert into grid_test values('building1','gridid1',1,'gridid1 x','gridid1 y','eci14',78);
insert into grid_test values('building1','gridid2',1,'gridid2 x','gridid2 y','eci21',98);
insert into grid_test values('building1','gridid2',1,'gridid2 x','gridid2 y','eci22',22);
insert into grid_test values('building1','gridid2',1,'gridid2 x','gridid2 y','eci23',19);
insert into grid_test values('building1','gridid2',1,'gridid2 x','gridid2 y','eci24',87);
insert into grid_test values('building1','gridid2',1,'gridid2 x','gridid2 y','eci25',78); select t10.buildingid,t10.gridid,t10.height,t10.gridx,t10.gridy,
t10.cell1,t10.cell1_mrcount,
t11.cell2,t11.cell2_mrcount,
t12.cell3,t12.cell3_mrcount
from
(
select buildingid,gridid,height,gridx,gridy,eci as cell1,total_num cell1_mrcount from
(
select * from
(
select buildingid,gridid,height,gridx,gridy,eci,total_num,row_number()over(partition by buildingid,gridid,height,gridx,gridy order by total_num desc) rn
from grid_test
group by buildingid,gridid,height,gridx,gridy,eci,total_num
) t10
where rn<=3
) t where rn=1
) t10
inner join
(
select buildingid,gridid,height,gridx,gridy,eci as cell2,total_num cell2_mrcount from (
select * from (
select buildingid,gridid,height,gridx,gridy,eci,total_num,row_number()over(partition by buildingid,gridid,height,gridx,gridy order by total_num desc) rn
from grid_test
group by buildingid,gridid,height,gridx,gridy,eci,total_num
) t10
where rn<=3
) t where rn=2
) t11 on t10.gridid=t11.gridid and t10.height=t11.height
inner join
(
select buildingid,gridid,height,gridx,gridy,eci as cell3,total_num cell3_mrcount from (
select * from (
select buildingid,gridid,height,gridx,gridy,eci,total_num,row_number()over(partition by buildingid,gridid,height,gridx,gridy order by total_num desc) rn
from grid_test
group by buildingid,gridid,height,gridx,gridy,eci,total_num
) t10
where rn<=3
) t where rn=3
) t12 on t10.gridid=t12.gridid and t10.height=t12.height buildingid gridid height gridx gridy cell1 cell1_mrcount cell2 cell2_mrcount cell3 cell3_mrcount
building1 gridid1 1 gridid1 x gridid1 y eci10 123 eci14 78 eci13 50
building1 gridid2 1 gridid2 x gridid2 y eci21 98 eci24 87 eci25 78
数据库“行专列”操作---使用row_number()over(partition by 分组字段 [order by 排序字段])的更多相关文章
- hive:数据库“行专列”操作---使用collect_set/collect_list/collect_all & row_number()over(partition by 分组字段 [order by 排序字段])
方案一:请参考<数据库“行专列”操作---使用row_number()over(partition by 分组字段 [order by 排序字段])>,该方案是sqlserver,orac ...
- 去重 ROW_NUMBER() OVER(PARTITION BY 分组字段 ORDER BY 排序字段) RN
关键字 ROW_NUMBER() OVER(PARTITION BY 分组字段 ORDER BY 排序字段) RN 按照分组字段进行排序并标编号 ROW_NUMBER() OVER(PARTITIO ...
- 【SQL】ROW_NUMBER() OVER(partition by 分组列 order by 排序列)用法详解+经典实例
#用法说明 select row_number() over(partition by A order by B ) as rowIndex from table A :为分组字段 B:为分组后的排序 ...
- oracle ROW_NUMBER() OVER(PARTITION BY '分组' ORDER BY '排序' DESC) 用法
转载:https://blog.csdn.net/dbagaoshou/article/details/51330829 SELECT * FROM ( SELECT ROW_NUMBER() OVE ...
- SELECT a.loginname,a.deviceid,a.time,Row_Number() OVER (partition by a.loginname ORDER BY a.deviceid desc,a.time asc) rank
现在做一个反欺诈内容要用到笛卡尔积,用来分析用户一个手机号,对应的多个设备,每个更换设备的时间,这里取的时间是系统收集时间,用来代表更换的时间, 所以要先对设备换的时间作排序,然后进行rank,最后求 ...
- row_number() over partition by 分组聚合
分组聚合,就是先分组再排序,可以的话顺手标个排名:如果不想分组也可以排名:如果不想分组同时再去重排名也可以 ROW_NUMBER() OVER( [PARTITION BY column_1, col ...
- sql 分组取最新的数据sqlserver巧用row_number和partition by分组取top数据
SQL Server 2005后之后,引入了row_number()函数,row_number()函数的分组排序功能使这种操作变得非常简单 分组取TOP数据是T-SQL中的常用查询, 如学生信息管理系 ...
- row_number和partition by分组取top数据
分组取TOP数据是T-SQL中的常用查询, 如学生信息管理系统中取出每个学科前3名的学生.这种查询在SQL Server 2005之前,写起来很繁琐,需要用到临时表关联查询才能取到.SQL Serve ...
- MYSQL-实现分组排序 对比 ORACLE 和SQLserver用 row_number() over(partition by ) 分组排序功能
以下是个人笔记: 本文是为了理解 row_number() over(partition by ) 和实现各种数据库的分组排序功能 select ROW_NUMBER()over( partitio ...
随机推荐
- 【Android】你知道还可以通过 View.animate() 来实现动画么
这次想来讲讲 View.animate(),这是一种超好用的动画实现方式,用这种方式来实现常用的动画效果非常方便,但在某些场景下会有一个坑,所以这次就来梳理一下它的原理. 基础 首先,先来看一段代码: ...
- EF Core利用Transaction对数据进行回滚保护
What? 首先,说一下什么是EF Core中的Transaction Transaction允许以原子方式处理多个数据库操作,如果事务已提交,则所有操作都应用于数据库,如果事务回滚,则没有任何操作应 ...
- 安装Oracle客户端和plsql
Oracle 客户端安装 + pl/sql工具安装配置 Oracle 客户端安装 + pl/sql工具安装配置 下载oracle客户端,并在本地安装. 11g下载地址为: http://www ...
- mac上Pycharm个性化快捷键,类似Myeclipse的快速复制等快捷键
好几天没写博客了,在win10下写了几天python,然后下了pycharm使用,发现还可以,但是太笨重了,切回了mac,然后装了pycharm社区版本. 但是这个使用太别扭了,没有myeclipse ...
- SuperMap iClient 查询成功后如何传递参数?
一.iClient API文档中的接口描述 二.范例 //定义一个this对象 this.param = new SuperMap.LonLat(point.x, point.y); querySer ...
- shell命令总结一
简述:这篇总结是在360企业安全实习第一周学到的. Linux中的 2>&1 .if文件命令 .tr .$0等相关参数含义的用法 1. 2>&1 command>/d ...
- [福大软工] W班 第2次成绩排行榜
作业链接 https://edu.cnblogs.com/campus/fzu/FZUSoftwareEngineering1715W/homework/866 评分细则 本次个人项目分数由三部分组成 ...
- Beta冲刺第二天
一.昨天的困难 没困难 二.今天进度 局部测试并修复出现的bug 1.林洋洋:修复登录页面显示问题,修复日程查询问题 2.黄腾达:修复创建协作开始时间和结束时间没做检验的问题 3.张合胜:修复页面内容 ...
- 在arc模式下 CGImage 释放问题
//大图bigImage //定义myImageRect,截图的区域 if (imagecount >= 3) { CGRect myImageRect; if (i.size.width< ...
- zookeeper提示Unable to read additional data from server sessionid 0x
配置zookeeper集群,一开始配置了两台机器server.1和server.2. 配置参数,在zoo.cfg中指定了整个zookeeper集群的server编号.地址和端口: server.1=1 ...