"StackOverflow problem Selection" in SQL to remove duplicate rows

Source: Internet
Author: User

problemSuppose you have a table with a larger amount of data (for example, 300,000+ rows), where there are duplicate rows (other than the primary key, the other column data is the same), how do you quickly go heavy? My watch looks like 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

Essence AnswerAssuming there are no null values, you can first group by the other columns, and then only Min or Max (RowId), to delete the 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 ROWID is not of type int, but is a GUID, you can use the


StackOverflow Link: http://stackoverflow.com/questions/18932/how-can-i-remove-duplicate-rows

"StackOverflow problem Selection" in SQL to remove duplicate rows

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.