Want to know how to eliminate duplicate records in sql? we have a huge selection of how to eliminate duplicate records in sql information on alibabacloud.com
Turn from Hibiscus bloghttp://blog.sina.com.cn/liurongxiu1211 SQL to remove duplicate statements(2012-06-15 15:00:01)SQL single table/multi-table query removes duplicate recordsSingle table DistinctMulti-table GROUP byGroup by must be placed before order by and limit, or it will be an error*****************************
Label:For example:ID Name Value1 a pp2 a PP3 B III4 B pp5 B pp6 C pp7 C pp8 C IIIID is primary keyAsk for such a resultID Name Value1 a pp3 B III4 B pp6 C pp8 C IIIMethod 1Deleteyourtablewhere [ID] not inch ( Select Max([ID]) fromyourtableGroup by(Name+value)) Method 2Deletea fromTable A Left Join( SelectId=min(ID) fromTableGroup byName,value) b ona.ID=b.idwhereb.ID is NULL SQL statement for querying and deleting
With SQL statements, deleting repeated items only keeps one record in thousands of records, and some identical records exist. How can I use SQL statements, delete duplicate records 1. Search for redundant
into duplicate_all
Select * from # tmp
(2) Use ROW_NUMBERCopy codeThe Code is as follows: with tmpAs(Select *, ROW_NUMBER () OVER (partition by c1, c2, c3 order by (getdate () as numFrom duplicate_allWhere c1 = 1)Delete tmp where num> 1
If multiple tables have completely repeated rows, you can use UNION to join multiple tables to a new table with the same structure, SQL Server helps remove duplicate rows b
duplicate fields are Name and address. You must obtain the unique result set of the two fields.Select Identity (INT, 1, 1) as autoid, * into # TMP from tablenameSelect min (autoid) as autoid into # tmp2 from # TMP group by name, autoidSelect * from # TMP where autoid in (select autoid from # tmp2)The last SELECT command gets the result set with no duplicate name and address (but an autoid field is added, w
Check the code first. You can first read the SQL statement to remove duplicate records and obtain duplicate records.Copy codeThe Code is as follows:ALTER procedure [dbo]. [PROC_ITEMMASTER_GETUNIQUE] @ pageindex int, @ uid int, @ itemnumber varchar (50)ASBegin tran -- start transactionDrop table [ItemMaster]. [dbo]. [te
SQL single table/multi-table query removes duplicate recordsSingle table DistinctMulti-table GROUP byGroup by must be placed before order by and limit, or it will be an error************************************************************************************1, look for redundant records in the table, duplicate
#tmp
From Duplicate_all
where C1 = 1
Go
Delete Duplicate_all where C1 = 1
Go
INSERT INTO Duplicate_all
SELECT * FROM #tmp
(2) using Row_number
Copy Code code as follows:
With TMP
As
(
Select *,row_number () over (PARTITION by c1,c2,c3 Order by (GETDATE ()) as Num
From Duplicate_all
where C1 = 1
)
Delete tmp where num > 1
If multiple tables have completely duplicate rows, consider combining m
records, one is a completely duplicate record, that is, all fields are duplicated records, second, some key fields duplicate records, such as the Name field is repeated, and the other fields may not be repeated or repeated can be ignored.1, for the first kind of repetition,
duplicate record, that is, all fields are duplicated records, second, some key fields duplicate records, such as the Name field is repeated, and the other fields may not be repeated or repeated can be ignored.1, for the first kind of repetition, easier to solve, usingSELECT DISTINCT * from TableNameYou can get a resul
SQL Single Table/multiple table query removal duplicate record single table distinct multiple table group byGroup byMust be placed inOrder byand limit before the error will be************************************************************************************1, duplicate records in the lookup table are judged by a si
For example:ID Name Value1 a pp2 a PP3 B III4 B pp5 B pp6 C pp7 C pp8 C IIIID is primary keyAsk for such a resultID Name Value1 a pp3 B III4 B pp6 C pp8 C IIIMethod 1Delete yourtablewhere [id] not in (Select MAX ([id]) from yourtableGroup BY (name + value))Method 2Delete aFrom Table a LEFT join (Select Id=min (ID) from table GROUP by Name,value) B on A.id=b.idwhere b.id is nullSQL statement for querying and deleting duplicate recordsSQL statement for
For example:ID Name Value1 a pp2 a PP3 B III4 B pp5 B pp6 C pp7 C pp8 C IIIID is primary keyAsk for such a resultID Name Value1 a pp3 B III4 B pp6 C pp8 C IIIMethod 1Delete yourtablewhere [id] not in (Select MAX ([id]) from yourtableGroup BY (name + value))Method 2Delete aFrom Table a LEFT join (Select Id=min (ID) from table GROUP by Name,value) B on A.id=b.idwhere b.id is nullSQL statement for querying and deleting duplicate recordsSQL statement for
# tmp(2) Use ROW_NUMBERCopy codeThe Code is as follows:With tmpAs(Select *, ROW_NUMBER () OVER (partition by c1, c2, c3 order by (getdate () as numFrom duplicate_allWhere c1 = 1)Delete tmp where num> 1
If multiple tables have completely repeated rows, you can use UNION to join multiple tables to a new table with the same structure, SQL Server helps remove duplicate rows between tables.
Ii. delete some
Thanks to Sun Yunan's summary, address http://www.cnblogs.com/sunxiaonan/archive/2009/11/24/1609439.htmlFor example:ID Name Value1 a pp2 a PP3 B III4 B pp5 B pp6 C pp7 C pp8 C IIIID is primary keyAsk for such a resultID Name Value1 a pp3 B III4 B pp6 C pp8 C IIIMethod 1Delete yourtablewhere [id] not in (Select MAX ([id]) from yourtableGroup BY (name + value))Method 2Delete aFrom Table a LEFT join (Select Id=min (ID) from table GROUP by Name,value) B on A.id=b.idwhere b.id is nullSQL statement fo
For example:Id name value1 a pp2 a pp3 B iii4 B pp5 B pp6 c pp7 c pp8 c iiiId is the primary keyThis result is required.Id name value1 a pp3 B iii4 B pp6 c pp8 c iii
Method 1Copy codeThe Code is as follows:Delete YourTableWhere [id] not in (Select max ([id]) from YourTableGroup by (name + value ))Method 2Copy codeThe Code is as follows:DeleteFrom table a left join (Select id = min (id) from Table group by name, value) B on a. id = B. idWhere B. id is null
SQ
For example:ID name value1 A PP2 A PP3 B III4 B PP5 B PP6 c pp7 c pp8 C IIIID is the primary keyThis result is required.ID name value1 A PP3 B III4 B PP6 c pp8 C III
Method 1Delete yourtableWhere [ID] Not in (Select max ([ID]) from yourtableGroup by (name + value ))
Method 2DeleteFrom table a left join (Select id = min (ID) from Table group by name, Value) B on A. ID = B. IDWhere B. ID is null
SQL statement for querying and deleting
Tags: src not tin cursor have compare people fetch lookup1 , duplicate records in the lookup table are judged by a single field (Peopleid).SELECT * from peoplewhere Peopleid in (select Peopleid from People GROUP by Peopleid have count (Peopleid) > 1) Example Two: Select * from TestTable where numeber in (theselect number from the People Group by number has a count (number) > 1) you can find the same num
A1, look for redundant records in the table, duplicate records are based on a single field (Peopleid) to determineSELECT * from Peoplewhere Peopleid in (select Peopleid from People GROUP by Peopleid have count (Peopleid) > 1)Example two:SELECT * FROM TestTablewhere Numeber in (select number from people group by number has count (number) > 1)You can find the same
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.