SQL Server Query statement filters duplicate data

Source: Internet
Author: User
Tags sql server query

Case one: There is a complete duplicate of the data in the table, that is, all field contents are the same

CREATE TABLE #

(User ID int, name varchar (10), age int)

INSERT INTO #

Select 111, ' John ', UNION ALL

Select 222, ' Dick ', UNION ALL

Select 333, ' Harry ', UNION All

Select 111, ' John ', 26

Method: SELECT DISTINCT * from #

Scenario 2: There are partial data duplicates in the table, where at least one field in the duplicate data does not repeat the CREATE TABLE # (user ID int, name varchar (10), age int, date datetime) insert INTO #select 111, ' John ', 2010-02-23 Union allselect 222, ' Dick ', 2010-03-13 Union Allselect 333, ' Harry ', 2011-03-25 Union allselect 111, ' John ', 26 2011-07-07 Method:--When two heavy, take the date of the big one

SELECT * FROM t a

Where NOT EXISTS (select 1 from t where a. User id= user id A. Name = name and date >a. Date)

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.