Query or delete duplicate records

DELETE a FROM transaction_records a WHERE  EXISTS(SELECT 1 FROM transaction_records WHERE inpdate='20171224' AND primary_info=a.primary_info AND ID<a.ID);

 

--Other references are as follows:

--Handle table duplicate records (query and delete)

/******************************************************************************************

 

************************************************************

1. For the duplicate value records with the same Num and Name, there is no size relationship and only one record is kept

2. When the Name is the same and the ID has a size relationship, keep one of the larger or smaller records

 

 

Date: 2008.06.06

*******************************************************************************************

 

***********************************************************/

 

--1, used to query duplicate records (if the column has no size relationship, 2000 is used to generate auto-increment columns and temporary table processing, and SQL2005 uses

 

row_number function processing)

 

--> --> (Roy) Generate test data

  

if not object_id('Tempdb..#T') is null

    drop table #T

Go

Create table #T([ID] int,[Name] nvarchar(1),[Memo] nvarchar(2))

Insert #T

select 1,N'A',N'A1' union all

select 2,N'A',N'A2' union all

select 3,N'A',N'A3' union all

select 4,N'B',N'B1' union all

select 5,N'B',N'B2'

Go

 

 

--I and Name have the same record with the smallest ID (recommended to use 1, 2, 3), method 3 is more efficient than 1, 2 in SQl05

method 1:

Select * from user a where not exists(select 1 from user where Name=a.Name and ID<a.ID)

 

Method 2:

select a.* from user a join (select min(ID)ID,Name from user group by Name) b on 

 

a.Name=b.Name and a.ID=b.ID

 

Method 3:

select * from user a where ID=(select min(ID) from user where Name=a.Name)

 

Method 4:

select a.* from user a join user b on a.Name=b.Name and a.ID>=b.ID group by 

 

a.ID,a.Name,a.Memo having count(1)=1 

 

Method 5:

select * from user a group by ID,Name,Memo having ID=(select min(ID)from user where 

 

Name=a.Name)

 

Method 6:

select * from user a where (select count(1) from user where Name=a.Name and ID<a.ID)=0

 

Method 7:

select * from user a where ID=(select top 1 ID from user where Name=a.name order by ID)

 

Method 8:

select * from user a where ID!>all(select ID from user where Name=a.Name)

 

Method 9 (Note: Available when the ID is unique):

select * from user a where ID in(select min(ID) from user group by Name)

 

--SQL2005:

 

Method 10:

select ID,Name,Memo from (select *,min(ID)over(partition by Name) as MinID from user a)T 

 

where ID=MinID

 

Method 11:

 

select ID,Name,Memo from (select *,row_number()over(partition by Name order by ID) as MinID 

 

from user a)T where MinID=1

 

Generate result:

/*

ID          Name Memo

----------- ---- ----

1           A    A1

4           B    B1

 

(2 rows affected)

*/

 

 

--II, Name with the same ID as the record, opposite to min:

method 1:

Select * from user a where not exists(select 1 from user where Name=a.Name and ID>a.ID)

 

Method 2:

select a.* from user a join (select max(ID)ID,Name from user group by Name) b on 

 

a.Name=b.Name and a.ID=b.ID order by ID

 

Method 3:

select * from user a where ID=(select max(ID) from user where Name=a.Name) order by ID

 

Method 4:

select a.* from user a join user b on a.Name=b.Name and a.ID<=b.ID group by 

 

a.ID,a.Name,a.Memo having count(1)=1 

 

Method 5:

select * from user a group by ID,Name,Memo having ID=(select max(ID)from user where 

 

Name=a.Name)

 

Method 6:

select * from user a where (select count(1) from user where Name=a.Name and ID>a.ID)=0

 

Method 7:

select * from user a where ID=(select top 1 ID from user where Name=a.name order by ID 

 

desc)

 

Method 8:

select * from user a where ID!<all(select ID from user where Name=a.Name)

 

Method 9 (Note: Available when the ID is unique):

select * from user a where ID in(select max(ID) from user group by Name)

 

--SQL2005:

 

Method 10:

select ID,Name,Memo from (select *,max(ID)over(partition by Name) as MinID from user a)T 

 

where ID=MinID

 

Method 11:

select ID,Name,Memo from (select *,row_number()over(partition by Name order by ID desc) as 

 

MinID from user a)T where MinID=1

 

Generate result 2:

/*

ID          Name Memo

----------- ---- ----

3 A A3

5           B    B2

 

(2 rows affected)

*/

 

 

 

--2. When deleting duplicate records has a size relationship, keep one of the large or small records

 

 

--> --> (Roy) Generate test data

 

if not object_id('Tempdb..user') is null

    drop table user

Go

Create table user([ID] int,[Name] nvarchar(1),[Memo] nvarchar(2))

Insert user

select 1,N'A',N'A1' union all

select 2,N'A',N'A2' union all

select 3,N'A',N'A3' union all

select 4,N'B',N'B1' union all

select 5,N'B',N'B2'

Go

 

--I, Name with the same record with the smallest ID (recommended to use 1, 2, 3), keep the smallest one

method 1:

delete a from user a where  exists(select 1 from user where Name=a.Name and ID<a.ID)

 

Method 2:

delete a  from user a left join (select min(ID)ID,Name from user group by Name) b on 

 

a.Name=b.Name and a.ID=b.ID where b.Id is null

 

Method 3:

delete a from user a where ID not in (select min(ID) from user where Name=a.Name)

 

Method 4 (Note: Available when the ID is unique):

delete a from user a where ID not in(select min(ID)from user group by Name)

 

Method 5:

delete a from user a where (select count(1) from user where Name=a.Name and ID<a.ID)>0

 

Method 6:

delete a from user a where ID<>(select top 1 ID from user where Name=a.name order by ID)

 

Method 7:

delete a from user a where ID>any(select ID from user where Name=a.Name)

 

 

 

select * from user

 

Generate result:

/*

ID          Name Memo

----------- ---- ----

1           A    A1

4           B    B1

 

(2 rows affected)

*/

 

 

--II, Name with the same ID keep the largest record:

 

method 1:

delete a from user a where  exists(select 1 from user where Name=a.Name and ID>a.ID)

 

Method 2:

delete a  from user a left join (select max(ID)ID,Name from user group by Name) b on 

 

a.Name=b.Name and a.ID=b.ID where b.Id is null

 

Method 3:

delete a from user a where ID not in (select max(ID) from user where Name=a.Name)

 

Method 4 (Note: Available when the ID is unique):

delete a from user a where ID not in(select max(ID)from user group by Name)

 

Method 5:

delete a from user a where (select count(1) from user where Name=a.Name and ID>a.ID)>0

 

Method 6:

delete a from user a where ID<>(select top 1 ID from user where Name=a.name order by ID 

 

desc)

 

Method 7:

delete a from user a where ID<any(select ID from user where Name=a.Name)

 

 

select * from user

/*

ID          Name Memo

----------- ---- ----

3 A A3

5           B    B2

 

(2 rows affected)

*/

 

 

 

 

 

--3, delete duplicate records when there is no size relationship, handle duplicate values

 

 

--> --> (Roy) Generate test data

  

if not object_id('Tempdb..user') is null

    drop table user

Go

Create table user([Num] int,[Name] nvarchar(1))

Insert user

select 1,N'A' union all

select 1,N'A' union all

select 1,N'A' union all

select 2,N'B' union all

select 2,N'B'

Go

 

method 1:

if object_id('Tempdb..#') is not null

    drop table #

Select distinct * into # from user--Exclude duplicate records result set to generate temporary table#

 

truncate table user--empty table

 

insert user select * from # --Insert temporary table # into table user

 

--View Results

select * from user

 

/*

Num         Name

----------- ----

1           A

2           B

 

(2 rows affected)

*/

 

--Use method 2 after re-executing the test data

Method 2:

 

alter table user add ID int identity--new identity column

go

delete a from  user a where  exists(select 1 from user where Num=a.Num and Name=a.Name and 

 

ID>a.ID)--retain only one record

go

alter table user drop column ID--delete the identity column

 

--View Results

select * from user

 

/*

Num         Name

----------- ----

1           A

2           B

 

(2 rows affected)

 

*/

 

--Use method 3 after re-executing the test data

Method 3:

declare Roy_Cursor cursor local for

select count(1)-1,Num,Name from user group by Num,Name having count(1)>1

declare @con int,@Num int,@Name nvarchar(1)

open Roy_Cursor

fetch next from Roy_Cursor into @con,@Num,@Name

while @@Fetch_status=0

begin 

    set rowcount @con;

    delete user where Num=@Num and Name=@Name

    set rowcount 0;

    fetch next from Roy_Cursor into @con,@Num,@Name

end

close Roy_Cursor

deallocate Roy_Cursor

 

--View Results

select * from user

/*

Num         Name

----------- ----

1           A

2           B

 

(2 rows affected)

 

 

#method 1:

  SELECT * FROM customer a WHERE a.customer_realname='李海锋' AND NOT EXISTS(SELECT 1 FROM 

 

customer WHERE customer_realname=a.customer_realname AND customer_id>a.customer_id AND 

 

customer_realname='Li Haifeng')

 

#Method 2:

  SELECT a.* FROM customer a JOIN (SELECT MAX(customer_id) AS customer_id FROM customer 

 

WHERE customer_realname='李海锋'GROUP BY customer_realname) b ON 

 

a.customer_id=b.customer_id  ORDER BY customer_id

 

#Method 3:

  SELECT a.* FROM customer a WHERE a.customer_realname='李海锋' AND  a.customer_id=(SELECT 

 

MAX(b.customer_id) FROM customer b WHERE b.customer_realname=a.custo

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326213301&siteId=291194637