SQL query duplicate data, delete duplicate data, filter duplicate data

Source: Internet
Author: User

SELECT * FROM (
SELECT Titleid,count (TitleID) c
from [Dragonguoshi]. [dbo]. [Articleinfo]
GROUP BY Titleid,[categorycode]
Having count (TitleID) >1) as T
ORDER BY C


Delete [Dragonguoshi]. [dbo]. [Articleinfo] where ID not in (
SELECT Max (ID) c
from [Dragonguoshi]. [dbo]. [Articleinfo]
Group by TitleID, [Categorycode]
)

SQL query statement filters for duplicate datasituation One: there is a fully duplicated data in the table, that is, all field contents are the sameCREATE TABLE #
(User ID int, name varchar (10), age int)
INSERT INTO #
Select 111, ' Zhang San ', union ALL
Select 222, ' John Doe ', UNION ALL
Select 333, ' Harry ', UNION All
Select 111, ' Zhang San ', 26

Method: SELECT DISTINCT * FROM # scenario 2: There is a partial data duplication field in the table, that is, there is at least one field in the repeating data that does not repeat the CREATE TABLE #
(User ID int, name varchar (10), age int, date datetime)
INSERT INTO #
Select 111, ' Zhang San ', 2010-02-23 UNION ALL
Select 222, ' John Doe ', 2010-03-13 UNION ALL
Select 333, ' Harry ', 2011-03-25 UNION ALL
Select 111, ' Zhang San ', 26 2011-07-07 Method:--When two heavy, fetch a date large select * from T awhere not exists (select 1 from t where a. User id= user ID a. Name = Name and date >a. Date)

SQL query duplicate data, delete duplicate data, filter duplicate data

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.