当前位置:网站首页>ORACLE分组排序后获取第一条和最后一条值
ORACLE分组排序后获取第一条和最后一条值
2022-07-17 05:20:00 【hello,world_yzs】
参考链接
实践
最近遇到的sql的问题。
wx_user的手机号有部分有问题,发生了串登录。原本一个用户一个手机号,现在变成了多个。
该用户的初始手机号是在登录履历表WX_USER_LOGIN中按照其身份证号查找的第一条(时间)。
根据以上分析,sql如下:
select u.id id,u.id_no idno,u.mobile wrongmobile,t.id_no tin, t.mobile truemobile from wx_user u,
( select id_no,mobile from (
select
t1.id_no,
t1.mobile,
row_number() over(PARTITION BY t1.id_no order by t1.create_time) rowss1
from (select * from WX_USER_LOGIN t where id_no in
(select id_no from wx_user where mobile in (
select mobile from wx_user group by mobile having count (*)>1))
) t1
)
where rowss1 = '1') t
where u.id_no in(t.id_no)
首先,揪出错误的手机号及对应的身份证:
select id_no from wx_user where mobile in (
select mobile from wx_user group by mobile having count (*)>1)
根据身份证号查出登录履历表:
select * from WX_USER_LOGIN t where id_no in
(select id_no from wx_user where mobile in (
select mobile from wx_user group by mobile having count (*)>1))
以上记录为表t1
进行分组排序:
select
t1.id_no,
t1.mobile,
row_number() over(PARTITION BY t1.id_no order by t1.create_time) rowss1
from t1
取分组排序后的第一条
select id_no,mobile from (
select
t1.id_no,
t1.mobile,
row_number() over(PARTITION BY t1.id_no order by t1.create_time) rowss1
from (select * from WX_USER_LOGIN t where id_no in
(select id_no from wx_user where mobile in (
select mobile from wx_user group by mobile having count (*)>1))
) t1
)
where rowss1 = '1'
分组排序后的第一条作为表t,联合原表和表t,得出所有需要的字段:
select u.id id,u.id_no idno,u.mobile wrongmobile,t.id_no tin, t.mobile truemobile from wx_user u,t where u.id_no in(t.id_no)
select * from a,b where a.id = b.id 意思就变了,此时就等价于: select * from a inner join b on a.id = b.id。即就是内连接。
后记
分组中用到PARTITION,它还有分区表的意思。接下来学习分区表。
边栏推荐
猜你喜欢

Eye tracking in virtual reality

Interview review nth time

Perceive the attention status of users on smart phones

感知智能手机上用户的关注状态

Robot stitching gesture recognition and classification
![Open source online markdown editor -- [editor.md]](/img/f3/b37acf934aa2526d99c8f585b6f229.png)
Open source online markdown editor -- [editor.md]

Positional change of the eyeball during eye movements: evidence of translational movement
![[force buckle] symmetric binary tree](/img/a4/31e936fb242aa8a3243ea257f5fd29.png)
[force buckle] symmetric binary tree

吴恩达机器学习第8-9章

[antdv: Each record in table should have a unique `key` prop,or set `rowKey` to an unique.....
随机推荐
Typescript learning
Set the index library structure, add suggestions that can be automatically completed to users, and turn some fields into collections and put them into suggestions
[force buckle] single valued binary tree
#MySql MySql 计算今年有多少天周末(周六、日)
Leetcode string
[force buckle] symmetric binary tree
Solutions to slow transmission speed of FileZilla virtual machine
DSL implements bucket aggregation
What's the worst programmer you've ever seen?
Restclient multi conditional aggregation
Es aggregation analysis reports an error: "reason": "text fields are not optimized for operations
用头部运动学习无姿态注视偏差
[force buckle] flip binary tree
实验一 简单程序设计
From entering URL to displaying page
Quelques concepts de base dans le réseau
Common serial communication UART seen from pictures
UDP的报文结构
Daily eye contact detection uses unsupervised gaze target detection
[force buckle] ring list II