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
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 III
Method 1
Copy Code code as follows:
Delete yourtable
where [id] not in (
Select MAX ([id]) from yourtable
Group BY (name + value)
Method 2
Copy Code code as follows:
Delete a
From Table a LEFT join (
Select Id=min (ID) from table GROUP by Name,value
) B on A.id=b.id
where b.id is null
= @idFetch cur_rows into @id, @maxEndClose Cur_rowsSET ROWCOUNT 0Method TwoThere are two meanings of duplicate records, one is a completely duplicate record, that is, all fields are duplicated records, and the second is some key field duplicate
When processing the database table data, there are often repeated data. In this case, there are often two duplicate records. There are many ways to delete duplicate records.
Program It can also be implemented using SQL statements. The following describes how to implement it
During the internship, a large amount of data was collected using the "thief" program, but many of them were repeated.
Delete the duplicate data (keep one record) according to the practice requirements, and delete several data tables
And put the data in a data table. Based on your own experience, let's take a few points.
1. Merge data tables
There is a select into statement in the SQL statement.
Copy or arc
-1872672 to 71-3249712 to 45-1867722-51-5454September 724-80-9391756-30-7391807-91-6654.846 to 92-7186899 to 46-2035998 to 72-3567
(19 row (s) affected)
The output of statements that involve distinct depends on the column sorting rule or the expression that uses distinct. For more information about the effects of different sorting rules, see basic knowledge of SQL Server sorting rules.
For the distinct keyword, each null value is considered to be a
by name, autoid
Select * from # TMP where autoid in (select autoid from # tmp2)
The last select gets the result set with no duplicate name and address (but an autoid field is added, and this column can be omitted in the select clause during actual writing). 3. Some key fields are repeated, the record has an ID. the first method can delete all repeated .. (only records with the smallest
SQL statement + select top usage of "Fetch 10th to 20th records from a data table", 10th SQL statements
1. First, select top usage:
For more information, see the differences between select top n * from and select * from.
Select * from table -- retrieve all data and return unordered Sets
Select top n * from table -- obtains the first n
With SQL statements, delete duplicates and keep only one
In thousands of records, there are some of the same records, how can you use SQL statements to delete duplicates?
1, look for redundant records in the table, duplicate
Select A.id,a.subject,a.receiver from Test1 a LEFT join (select C.subject,c.receiver, Max (c.id) as bid from Test1 C where Status=0 GROUP by Receiver,subject has count (1) >1) b on a.id
2. Delete duplicate records, keep only one record. Note that subject,receiver to index, otherwise it will be slow.
The code is as follows
Copy Code
Delete A from test1 A, (
as follows:
SELECT DISTINCT * into #Tmp tablename
DROP TABLE TableName
SELECT * INTO TableName from #Tmp
drop table #Tmp
This duplication occurs because the table is poorly designed and can be resolved by adding a unique index column.
2. This type of repetition usually requires the first record in the duplicate record to be retained, as follows
Suppose there is a duplicate field of n
Use SQL to query duplicate recordsCode
1. Search for redundant duplicate records in the Table. duplicate records are determined based on a single field (peopleid ).Select * from peopleWhere peopleid in (select peopleid from
This article introduces how to use havinggroupby and various selectin joint queries to filter the SQL language names of Repeated Records in different ways.
This article introduces how to use having group by and various select in joint queries to filter the SQL language names of Repeated Records in different ways.
1.
It means you have duplicate records in your data.Two cases1. Existing tables and duplicates to import dataThis can be done by establishing an association between two tables, and the primary key cascade to find duplicate records2. Duplicate records exist in the table to be im
Delete a duplicate data SQL statementMethod One
Suppose there is a duplicate field of name,address that requires a unique result set for both fieldsSelect 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)
Method TwoDeclare @
First the code, you can see the SQL statement to remove duplicate records, get duplicate records
Copy Code code as follows:
ALTER procedure [dbo]. [Proc_itemmaster_getunique] @PAGEINDEX int, @uid int, @itemnumber varchar (50)
As
Begin Tran--Start a transa
For example, there is a personnel table (table name: peosons)
Select P1. * from persons P1, persons P2 where p1.id The above results can be achieved.
Several SQL statements used to delete Repeated Records
1. Use the rowid Method
2. Use the group by Method
3. Use the distinct method
1. Rowid Method
The statement is as follows:
Query data: Select * From Table1 A where rowid! = (Select max (rowid)
SQL statement for querying and deleting duplicate records1. Search for redundant duplicate records in the Table. duplicate records are determined based on a single field (peopleid ).Select * from peopleWhere peopleid in (select pe
problem usually requires to keep the first record in the duplicate record, the operation method is as followsSuppose there is a duplicate field name,address, which requires the result set to be unique for both fieldsSelect Identity(int,1,1) asAutoid,* into#Tmp fromTableNameSelect min(autoid) asAutoid into#Tmp2 from#TmpGroup byname,autoidSelect * from#TmpwhereAutoidinch(SelectAutoid from#tmp2)The last se
SQL statement for mysql to delete duplicate data and mysql to delete data
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, title, name, content having count (*)> 1 ); delete from get_review_url WHERE
Webjxcom prompt: for example, if you want to query records with the same name, ID card number, and address in the current table (table name: peosons), select P1. * from persons P1, persons P2 where p1.idp2. ID and p1.cardid = p2.cardid and p1.pname = p2.pname and p1.address = p2.address. quantity
For example, there is a personnel table (table name: peosons)If you want to query records whose names, ID
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.