sql查询删除重复数据

数据库UserInfo

删除重复数据 即删除重复的用户名手机号 同一个用户名手机号只保留一个用户

01、根据多个字段查询重复数据

with data1 as(

select MobilePhone,Name from UserInfo
group by MobilePhone,Name
having count(*)>1

),

02、对重复数据分配编号

data2 as (

select u.*,row_number() over(partition by u.MobilePhone,u.Name order by Id ) rowNum from UserInfo u
inner join data1 d on u.Name=d.Name and u.MobilePhone=d.MobilePhone

),

03、查询需要删除数据的主键(每个小组里的编号为一的保留 其它删除)

data3 as(

select Id from data2 where rowNum>1

)

04、根据主键删除数据

delete from UserInfo
where Id in (select Id from data3)

删除后

附 sqlserver中分区函数partition by的用法 http://www.cnblogs.com/zhangchengye/p/5473860.html

时间: 2024-10-11 22:09:01

sql查询删除重复数据的相关文章

SQL语句删除重复数据

1.如表中没有主键,先添加自动增长主键 alter table 表名 add 列名 int identity (1,1) primary key 2.删除重复数据 delete from 表名 where id not in (select min(id) from 表名 group by id)

SQL 查询不重复数据

sql不重复的查找统计数据2007-06-29 11:04例表如下: 表名:MYTEST TID    COL1    COL2     COL3 1           1           A            A 2           1           A            A 3           2           A            A 4           2           B            A 5           3       

sql查询不重复数据

SELECT * FROM user WHERE Uid IN ( select min(Uid) from user group by Sid ) 原文地址:https://www.cnblogs.com/gjack/p/10326651.html

mysql 删除重复数据的sql语句

CREATE TABLE tmp AS SELECT id FROM get_review_url WHERE (no,title,name,content) IN (SELECT no,title,name,content FROM get_review_url GROUP BY no,title,name,content HAVING COUNT(*) > 1) AND id NOT IN (SELECT MIN(id) FROM get_review_url GROUP BY no,tit

orcl数据库查询重复数据及删除重复数据方法

工作中,发现数据库表中有许多重复的数据,而这个时候老板需要统计表中有多少条数据时(不包含重复数据),只想说一句MMP,库中好几十万数据,肿么办,无奈只能自己在网上找语句,最终成功解救,下面是我一个实验,很好理解. ------------------------------------------------------------------------------------------------------------------------ 假设有一张人员信息表cs(姓名,证件号,地址

数据库删除重复数据 sql

delete from `user`where id in( SELECT * from(select id from `user` WHERE id not in (select min(id) from `user` group by names having count(names)>1) ) tep) 红色为查询出重复数据的id作为要删除的数据条件 原文地址:https://www.cnblogs.com/libiao-libar/p/9744700.html

sql删除重复数据

--删除重复数据delete from PMS_Contract_LeasedRoom  where LeasedRoomId not in(select max(LeasedRoomId) from PMS_Contract_LeasedRoom group by ContractCode,RoomCode) --having count(*)>1)

MYSQL删除重复数据

delete from co_jobinformation cwhere c.name in (select cc.name from co_jobinformation cc group by  cc.name   having count(cc.name) > 1)and rowid not in (select min(rowid) from co_jobinformation e group by e.name having count(e.name )>1) 之前在oracle数据库

mysql 删除重复数据,并保存最新一条数据

删除重复行 1 DELETE FROM ecm_member_login_session 2 WHERE (number , client_code) IN ( 3 SELECT number, client_code FROM mall.ecm_member_login_session GROUP BY number , client_code HAVING COUNT(*) > 1) 4 AND update_time NOT IN ( 5 SELECT MAX(update_time) F