#T a where exists (select 1 from #T where Num=a.num and Name=a.name and id>a.id)--keep only one recordGoALTER TABLE #T drop column id--Delete identity column--View ResultsSELECT * FROM #T/*Num Name----------- ----1 A2 B(2 rows affected)*/--use Method 3 after re-executing test dataMethod 3:Declare roy_cursor Cursor Local forSelect COUNT (1) -1,num,name from #T GROUP by Num,name have count (1) >1declare @con int, @Num int, @Name nvarchar (1)Open Roy_cu
The question of how to dynamically number rows in SQL, plus line numbers, is a classic problem in database queries. I'll sort out the present method and share the skills. The code is based on the pubs boilerplate database. In SQL, these two methods are generally:
1. Use of temporary tables
You can use SELECT into to create a temporary table, in the first column
SQL Server converts rows to columns: for example, if a table contains only a small amount of data, you can use static SQL statements such as selectstuName and max (caseCoursewhen language thenscoreelse0end) as language, max (caseCoursewhen math thenscoreelse0end) as math, max (caseCoursewhen
SQL Server transfers
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
Deleting duplicate rows on SQL Server is one of the most common operations. The following describes six methods that can be used to delete duplicate rows on SQL Server for your reference.
1. If an ID field exists, it is a unique f
Each time you import an Excel table to the database, several more rows are displayed as 'null' rows. The following prompt is displayed when you delete the table.
"The key column information is insufficient or incorrect. Update affects multiple rows! "
Solution: You can see from your description that you have imported data from Excel to
How to quickly delete duplicate records in SQL Server
Developer nightmare-delete Repeated Records
Presumably, every developer has had a similar experience. When querying or collecting statistics on the database, the query and statistics results may be inaccurate due to repeated records in the table. The solution to this problem is to delete these duplicate
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
There are often repeated records in the database. sometimes I want to query duplicate records and display them, and sometimes I want to delete them. next I will introduce how to delete and query duplicate records.
There are often repeated records in the database. sometimes I want to query duplicate records and display them, and sometimes I want to delete them. ne
server| Repeat | Recurring 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
table tmp_wrap;Query OK, 0 rows affected (0.05 sec)
2. There is also a very streamlined approach.
Find the duplicate and remove the smallest one.
The Code is as follows:
Delete users_groups as a from users_groups as,(Select *, min (id) from users_groups group by uid having count (1)> 1) As BWhere a. uid = B. uid and a. id> B. id;(7 row (s) affected)(0 MS taken) Query result
-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
1. Find all duplicate records
The code is as follows
Copy Code
SELECT * FROM table Where repeat field in (Select repeating field from table Group by repeating field having Count (*) >1)
2. Filter duplicate records (show only one)
The code is as follows
Copy Code
SELECT * from Hzt Where ID in (select Max (ID) from Hzt Group by Title)
This article will introduce to you in detail the summary of SQL statement methods for deleting duplicate records in SQLServer. In general, we will delete the records in two cases, one is a completely repeated record, where all fields are repeated, and the other is a record with duplicate key fields. other fields are not repeated or repeated, but can be ignored.
T
There are many ways to delete duplicate records. Here I summarize a variety of SQL statements for deleting restart records, such as deleting duplicate id data and searching for duplicate records, remove the smallest record, delete the repeat record, retain only one record and delete the redundant record in the table. T
table group by name)Delete from table where field in (Select field from Table group By field has count (*) > 1)The above method is also useful when deleting a small order of data, and when the data is processed by hundreds of thousands of or more, the general machine estimates that it will be paid as soon as it runs. In fact, a little bit of common sense is to know that such a statement will have a large amount of computation, and its operation is at least in the form of a exponentiation, think
figure 1, the records of product Chang and Tofu are repeated in the product information table. To delete these duplicate records, only one of them is retained. The procedure is as follows:
The first axe -- create a temporary table with the same structure
Create table Products_temp (ProductID int,ProductName nvarchar (40 ),Unit char (2 ),UnitPrice money)
Second Board ax-add an index to the table and make it ignore
Problems that occur when you update a table that does not have a primary key set:Cause of the problem:Most of this is due to the absence of a primary key (PK) that causes several identical data to exist in the same tableWhen the DBMS is stored, only one piece of data is stored, because the DBMS is optimized to reduce data redundancy. So deleting or updating a duplicate data is reachingWorkaround:It is recommended to set the primary key to check for th
Developer nightmare-delete Repeated Records
Presumably, every developer has had a similar experience. When querying or collecting statistics on the database, the query and statistics results may be inaccurate due to repeated records in the table. The solution to this problem is to delete these duplicate records and keep only one of them.
In SQL Server, apart from manual deletion of tables with more than a d
Developer nightmare-delete Repeated RecordsPresumably, every developer has had a similar experience. When querying or collecting statistics on the database, the query and statistics results may be inaccurate due to repeated records in the table. The solution to this problem is to delete these duplicate records and keep only one of them.In SQL Server, apart from manual deletion of tables with more than a doz
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.