Alibabacloud.com offers a wide variety of articles about remove duplicate rows in sql, easily find your remove duplicate rows in sql information here online.
want to use a statement query to get all the data that name does not duplicate, then you must use distinct to remove the extra duplicate records. The result of the select distinct name from table is that the name a C seems to have worked, but I What do you want to get the ID value? Change the query statement bar: SELECT DISTINCT name, id from table
One of the problems in the project last week was to get the value of a column in the DataTable, since retrieving data from a database is grouped by 2 fields, and the column to be obtained is exactly in the two columns of the grouping, so the value of the column is bound to be duplicated, so I think of removing duplicates, With the idea after the online look at some of the methods, mostly traversal, although the function can be achieved, but the efficiency is too low, and finally found a simple m
Want to arrange the list of questions according to the latest answers. But the questions and answers are split up in two tables to store.Therefore, to complete the above requirements, you need to display the data from the main table "problem", but you have to sort by the update date of the second table (answer).Using join to do, always cannot remove repetition,Toss for two days, finally still have to use GROUPBY (affect performance of East).Duplicates
Multiple table queries to remove duplicate records
First of all about SQL multiple table query to remove duplicate records we can think of using group by or distinct again to think of inner left,Let's look at an example
Look at a distinct instance
Now release the full sta
use a statement query to get the name does not duplicate all the data, it must use distinct to remove unnecessary duplicate records.
select DISTINCT name from table
the results obtained are:Name
A
C
It seems to be working, but what I want to get is the ID value? Change the query statement:
SELECT DISTINCT name, ID from table
The result will be:ID Name
1 A
2 b
(NOLOCK) group by TID,SN have COUNT (0) >1Delete Dorecordproperty from#Tmp p where P.tid=dorecordproperty.tid and p.sn=dorecordproperty.sn and Dorecordproperty.idSelect COUNT (0) as num from #Tmp--query data for temporary tablesTRUNCATE TABLE #Tmp-Clears all data and constraints for the staging tabledrop table #TmpSET NOCOUNT OFF Sure enough, in 40 seconds, the execution was successful. The above is on the server: CPU 2u e5-2620; memory 96G HDD is 1T enterprise level 7200 RPM; Data is operati
SQL Duplicate record query
1, look for redundant records in the table, duplicate records are based on a single field (Peopleid) to judge
SELECT * from people
where Peopleid to (select Peopleid from People GROUP by Peopleid have count (Peopleid) > 1)
Case Two:
SELECT * from TestTable
where Numeber to (select number from people group by nu
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
#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
# 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
emp_name salary
------------------------------------------------------------
1 sunshine 10000
2 semon 20000
SQL> select * from employee E1
Where rowid in (select max (rowid) from employe E2Where e1.emp _ id = e2.emp _ id and
E1.emp _ name = e2.emp _ name and e1.salary = e2.salary );
Emp_id emp_name salary
------------------------------------------------------------
1 sunshine 10000
3. XYZ 30000
2 semon 20000
2. delete several methods:
(1) create a
Example of deleting duplicate records in a data table in SQL Server, SQL Server
[Project]
The users table in the database contains the u_name and u_pwd fields. The u_name contains duplicate items. Now we need to delete duplicate items!
[Analysis]
1. Generate a temporary tabl
Quickly delete duplicate records in SQL Server 2006-07-17 21:53:15Category: SQL Server
Developer's Nightmare-delete duplicate recordsPresumably, every developer has had a similar experience, when querying or statistics of the database will occasionally encounter due to the existence of
new record, the value of the affected row is 1. If the original record is updated, the value of the affected row is 2.For more information about the insert into... on duplicate key function, see MySQL Reference Documentation: 13.2.4. INSERT syntax.Now the question is, how can I specify the value of the field after ON DUPLICATE KEY UPDATE if multiple rows are ins
There are many ways for SQLServer to search for and delete duplicate records in tables. Below I will list several commonly used SQL statements with good performance. If you need them, please refer to them.
There are many ways for SQL Server to find and delete duplicate records in tables. Below I will cite several commo
SQL statements remove duplicate records, get duplicate records
--Query a table to effectively remove duplicate records, UserID as a self-growing primary key, Roleid as a repeating field
The code is as follows
Cop
Table 2 as B on. primary Key = B. primary Key ======= Article 6 ========== 1. query repeated records in the database: Select realname, count (*) from Users Group by realname having count (*)> 1 ========= Article 7 =========== select t0.itemcode, t0.itemname from oitm t0 where exists (select 1 from Oitm A where. codebars =. codebars and. itemcode BC
D111
A34
Bvb222
A35
Fgg333
DHT
Sdf444
A345
De555
Csfsf
Sscv666
BRT
FG, let's take a look at the
object_id (' tempdb.. #tmp ') is not null
drop table #tmp
Go
SELECT DISTINCT * into #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
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
(SELECT * from TB pivot (MAX (score) for course in (language, mathematics, physics)) m,
(select name, sum (score) Total, Cast (avg (fractional *1.0) as DECIMAL (18,2)) is the average of the from
TB
GROUP by name N
WHERE m. name =n. Name 4. Using SQL Server 2005 Dynamic SQL --using stuff ()-
-
DECLARE @sql VARCHAR (8000)
SET @
Nightmare for Developers--delete duplicate records Presumably every developer has had a similar experience, and when querying or counting the database, the query and statistic results are inaccurate due to duplicate records in the table. The solution to this problem is to delete the duplicate records and keep only one of them. in
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.