[Stackoverflow good question] Remove duplicate rows in SQL, stackoverflowsql

Source: Internet
Author: User

[Stackoverflow good question] Remove duplicate rows in SQL, stackoverflowsql
ProblemSuppose there is a table with a large data volume (for example, 300,000 + rows) with duplicate rows (except for the primary key, the data in other columns is the same). How can we quickly remove duplicates? My table is similar to this

MyTable-----------RowID int not null identity(1,1) primary key,Col1 varchar(20) not null,Col2 varchar(2048) not null,Col3 tinyint not null

Excellent answerIf there is no null value, you can first perform group by on other columns, then only retain MIN or MAX (RowId), and delete other rows:
DELETE MyTable FROM MyTableLEFT OUTER JOIN (   SELECT MIN(RowId) as RowId, Col1, Col2, Col3    FROM MyTable    GROUP BY Col1, Col2, Col3) as KeepRows ON   MyTable.RowId = KeepRows.RowIdWHERE   KeepRows.RowId IS NULL
If the RowId is not of the int type but a GUID, you can use
CONVERT(uniqueidentifier, MIN(CONVERT(char(36), MyGuidColumn))) 


Stackoverflow link: http://stackoverflow.com/questions/18932/how-can-i-remove-duplicate-rows
Column introduction: I like stackoverflow very much and can always find a solution to the problem. The website has a popularity list. So I selected some hot questions and sorted out the discussion based on my own understanding. Therefore, these articles are not really translated, but have made some additions, deletions, and retouching based on your own understanding. I hope that the above discussions can be shared with you more concisely and effectively. If you need to reprint, please specify the original addressHttp://blog.csdn.net/lizeyang

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.