SQL SERVER Deletes duplicate content rows _mssql

Source: Internet
Author: User
Tags log log
For repeated row deletion problem, the Internet is difficult to find the right answer, ask a lot of questions, but in the search engine cursory look at the previous record there is no solution.
In fact, this problem can be very gorgeous solution.
1, if the table does not have a primary key (or the same row does not have a different content column), you need to create a new self column, to distinguish between different columns. For example
Copy Code code as follows:

ALTER TABLE [tablename] add [TID] int IDENTITY (1,1)

is to increment the temporary column tid.

Why do you use SQL statements? If more than hundreds of thousands of lines are used, the design interface modification of SQL Server Enterprise Manager usually times out.

2, then is the key. An example is the list of IP addresses that I have in hand to handle. Nearly 400,000 data, there are SIP and EIP (Start IP and end IP) record repeat, the number of duplicates is about 1/5. This situation is done with a simple SQL command:
Copy Code code as follows:

Delete from query_ip where TID isn't in (select Max (TID) from QUERY_IP Group by SIP,EIP)

The SIP and EIP are grouped together to obtain the largest TID value of the same grouping. Then delete the contents of the original table that are not in it (that is, the smaller ID content in the same group of duplicates).
This way of thinking can extend a lot of SQL solution. For example, a user landing table, to see each user's recent log log.

A very elegant query statement:
Copy Code code as follows:

SELECT * from Loginlog where ID in (select Max (ID) from Loginlog Group by UserID)

SQL is powerful, and many complex requirements can often be merged into a single SQL statement query. Therefore, in my program, in addition to Update/insert and other operations require transaction support, or the record is too large to need a paging or temporary table. is usually implemented with a single SQL statement. For example, select *, (SELECT COUNT (*) from XXX where xxx=t.id) from t where .... This allows the associated statistics to be obtained in the SELECT statement, especially for small to medium systems.
Related Article

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.