Here is the thing, if we have a table with the following structure, there are thousands of records in this table, and probably some of which is duplicated. Now we need to delete those duplications by a sql query, what we should do? 

Remove duplicate rows from a tableCREATE TABLE [dbo].[postings] (
Remove duplicate rows from a table    
[ID] [int] IDENTITY (11NOT NULL ,
Remove duplicate rows from a table    
[postingname] [varchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
Remove duplicate rows from a table    
[postedby] [varchar] (20) COLLATE Chinese_PRC_CI_AS NULL
Remove duplicate rows from a table

There are many different ways can accomplish this, like cursor, tempary table, concatenate columns together and then use distinct operator etc. But here I'm gonna tell you another easy way.

Remove duplicate rows from a tableDELETE FROM postings
Remove duplicate rows from a table
WHERE EXISTS 
Remove duplicate rows from a table(
Remove duplicate rows from a table    
SELECT NULL FROM postings b
Remove duplicate rows from a table    
WHERE
Remove duplicate rows from a table        b.
[postingname] = postings.[postingname] AND 
Remove duplicate rows from a table        b.
[postedby] = postings.[postedby]
Remove duplicate rows from a table    
GROUP BY
Remove duplicate rows from a table        b.
[postingname], b.[postedby]
Remove duplicate rows from a table    
HAVING
Remove duplicate rows from a table        postings.
[id] < MAX(b.[id])
Remove duplicate rows from a table)

Hope it helps.

相关文章: