一、场景描述: 项目中的定时任务用于清除重复数据
delete from t_b_count t where t.id in (
select id from (
select t1.*,row_number()
over(partition by t1.order_code,t1.add,t1.delete,t1.update order by 1) rn
from t_b_count t1
where substr(t1.ftp_date,1,6)=to_char(sysdate-1,'yyyyMM')
)t2 where t2.rn > 1
)
二、注意上面 rn 代表按全字段分区后,相同类型数据出现的次数,因为采用的是全字段分区,所以t2.rn>1的数据必定是重复数据,需要删除
三、其它函数说明
按课程分类,分别查出学生的分数排名
select t.name,t.class,t.sroce,rank()
over(partition by t.class order by t.sroce desc) mm
from T2_TEMP t;
结果如下:
name 课程 分数 mm(即出现次数)
dss 1 95 1
ffd 1 95 1
fda 1 80 3
gds 2 92 1
cfe 2 74 2
gf 3 99 1
ddd 3 99 1
3dd 3 78 3
asdf 3 55 4
adf 3 45 5
- row_number(): 当有人成绩相同时,只会取第一个
- rank(): 跳跃显示,会显示并列的全部数据,但是排名会跳跃,例如:1-1-3
- dense_rank(): 同上,但是排名是连续的,不会跳跃。例如:1-1-2
本文详细介绍了如何使用SQL语句清除数据库中的重复数据,通过实例展示了使用row_number()函数结合子查询来筛选并删除重复记录的方法。此外,还深入解析了rank()和dense_rank()函数在数据排名中的应用,包括它们在处理相同成绩时的不同表现。
3098

被折叠的 条评论
为什么被折叠?



