1、原理讲解-可直接看2
筛选出最新的
SELECT *
FROM (
SELECT t.*,
ROW_NUMBER() OVER (PARTITION BY LOCALAUTHID ORDER BY LASTUPDATETIME DESC) AS rn
FROM USER_LOCALAUTH_STATE t
) t
WHERE t.rn = 1;
解释:
这个序号是基于[LOCALAUTHID]字段进行分区的(即每个[LOCALAUTHID]值都是一个新的分区),并且在每个分区内部,行是按照LASTUPDATETIME
字段降序排列的
2、在重复数据取最新的
SELECT *
FROM (
SELECT t.*,ROW_NUMBER() OVER (PARTITION BY LOCALAUTHID ORDER BY LASTUPDATETIME DESC) AS rn
FROM
(
-- 筛选存在重复的数据,count(LOCALAUTHID) > 1
select * from USER_LOCALAUTH_STATE
where LOCALAUTHID in (
SELECT LOCALAUTHID
FROM USER_LOCALAUTH_STATE WHERE STATE = 1 AND BUSINESSTYPE=1
group by LOCALAUTHID having count(LOCALAUTHID) > 1)
) t
) rt
WHERE rt.rn = 1;
3、若要删除不满足条件
在2的基础上,通过[LOCALAUTHID]查询,并删除ID不再2中的