当前位置:网站首页>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,它还有分区表的意思。接下来学习分区表。
边栏推荐
- 机器学习篇-逻辑回归的分类预测
- Talking about several solutions of cross domain
- [force buckle] maximum depth of binary tree
- 实验四 运算符重载和虚函数
- 使用候选选择从人类注视中学习视频显著性
- Experiment 4 operator overloading and virtual functions
- Volatile function of embedded C language
- Leetcode tree
- Color histogram grayscale image & color image
- EOG based eye movement detection and gaze estimation for an asynchronous virtual keyboard
猜你喜欢

SalGaze:使用视觉显著性的个性化注视估计

无80和443端口下申请域名SSL证书(适用于 acme.sh 和 certbot)

Unity2d learning Fox game production process 1: basic game character control, animation effects, lens control, item collection, bug optimization

虚拟现实中的眼睛跟踪

Positional change of the eyeball during eye movements: evidence of translational movement

Depth first search (DFS for short)

吴恩达机器学习第6-7章

感知智能手機上用戶的關注狀態

Operation of documents in index library

Solution: unable to load file c:\program files\ Because running scripts is forbidden on this system
随机推荐
Baby Ehab partitions again (DP, construction, bit operation)
Quelques concepts de base dans le réseau
Restclient multi conditional aggregation
Where have all the older programmers gone?
UDP的报文结构
Convert PNG with transparency to C array
感知智能手機上用戶的關注狀態
单表查询、添加、更新与删除数据
TCP protocol
Experiment 4 operator overloading and virtual functions
Busybox 1.21.1 has udpsvd function, which can be compiled successfully without interfering with the local busybox method
JS variable promotion
手把手搭建家用 NAS 全能服务器(1)| 配置选择及准备
第五章 我们可以直接调用run()方法来启动一个新线程吗
Es aggregation analysis reports an error: "reason": "text fields are not optimized for operations
虚拟现实中的眼睛跟踪
网络中的一些基本概念
C language calls the file browser to realize the effect of selecting files
[antdv: Each record in table should have a unique `key` prop,or set `rowKey` to an unique.....
Busybox specified date modification temporarily does not require clock -w to write to hardware