SQL SERVER deletes duplicate content rows

Source: Internet
Author: User

For repeated row deletion problems, it is difficult to find a suitable answer on the Internet and there are a lot of questions, but there is no solution to the previous record in the search engine.
In fact, this problem can be effectively solved.
1. If the table does not have a primary key (or the same row does not have different content columns), you need to create an auto-increment column to distinguish different columns. For example Copy codeThe Code is as follows: alter table [tablename] add [TID] int IDENTITY (1, 1)

Is to add an incremental temporary column TID.

Why SQL statements? If the number of rows exceeds 100,000, modification to the design interface of the SQL SERVER Enterprise Manager usually times out.

2. Then it is the key. Here is an example of the IP address location list that I want to handle. There are nearly 0.4 million pieces of data, with repeated records of SIP and EIP (start IP and end IP). The number of duplicates accounts for about 1/5. To solve this problem, use a simple SQL command:Copy codeThe Code is as follows: delete from query_IP where TID not in (select max (TID) from query_IP group by SIP, EIP)

That is, the maximum TID value of the same group is obtained by grouping the SIP and EIP. Then, delete the content that is not in the original table (that is, smaller ID content in the duplicate content of the same group.
This idea can be used to extend a lot of ways to solve SQL problems. For example, a user login table needs to view the recent login records of each user.

An elegant query statement:Copy codeThe Code is as follows: Select * from LoginLog where ID in (select max (ID) from LoginLog group by UserID)

SQL is extremely powerful. Many complex requirements can be combined into a single SQL statement query. Therefore, in my program, except for the UPDATE/INSERT operations, transaction support is required, or the number of records is too large, which requires paging or temporary tables. It is usually implemented using an SQL statement. For example, select *, (select count (*) from xxx where xxx = t. ID) from t where .... In this way, the associated statistical items can be obtained in the SELECT statement, which is especially useful for small and medium systems.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.