Only the first and first of the duplicate data are retained.

Source: Internet
Author: User

Only the first and first of the duplicate data are retained.

-- CREATE test TABLE [dbo]. [testtab] ([id] [nchar] (10) NULL, [name] [nchar] (10) NULL ); -- insert test data into the test table: insert into testtab values ('1', '1'); insert into testtab values ('1', '1 '); insert into testtab values ('2', '2'); insert into testtab values ('2', '2'); insert into testtab values ('3 ', '3'); insert into testtab values ('3', '3'); -- create a temporary table, insert data in the test table testtab to the temporary table, and add an auto-increment id: autoID select identity (int,) as autoID, * into # Tmp from testtab -- delete temporary table # repeated data in tmp according to autoID, only the first delete # Tmp where autoID in (select max (autoID) from # Tmp group by id) in each group of duplicate data is retained; -- delete testtab of all data in the testtab table; -- insert # insert into testtab select id, name from # Tmp; -- delete temporary table # Tmp drop table # Tmp;

How can I delete a duplicate row in SQL database and keep a record? Oh

Let's take a look at your table structure. Let me write a simple article for your reference:
Delete From table
Where Id not in (Select min (id), column1, column2 From table group by column1, column2)

The SQL database has multiple records that are completely duplicated. How can we delete duplicate records and keep only one record? How can we write SQL statements?

Delete from tablename where id not in (select max (id) from

Tablename group by col1, col2 ,...)

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.