select * from table_name WHERE id in ( select max(id) id from table_name GROUP BY 分组字段)
SELECT r.* FROM records r INNER JOIN ( SELECT group_id, MAX(timestamp) as latest FROM records GROUP BY group_id ) as latest_records ON r.group_id = latest_records.group_id AND r.timestamp = latest_records.latest;
筛选出最新的
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字段降序排列的
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;
在2的基础上,通过[LOCALAUTHID]查询,并删除ID不再2中的
接到任务,查询2021年之前的US_ADDCONSIGN表中用户信息。
同时同事告知,有用户重复注册所以信息有重复的,要求重复的信息中获取目前仍有效,且注册时间最早的那一条记录。
select * from (select row_number() over (partition by certno order by consigndate) rn, a.* from US_ADDCONSIGN a where 1=1 and state=1 ) where rn=1 and to_date(consigndate,'yyyymmdd')<to_date('20210101','yyyymmdd');
到此这篇关于oracle/mysql数据库多条重复数据如何取最新的文章就介绍到这了,更多相关sql多条重复数据取最新内容请搜索插件窝以前的文章或继续浏览下面的相关文章希望大家以后多多支持插件窝!