Possible Duplicate:
SQL - How can I remove duplicate rows?
SQL query to delete duplicate rows from same table?
How to find duplicity for example in this table?
Column A is unique ID and columns E and F are irrelevant, so rows 1,2,3 and rows 4,5 are duplicates
SQL Delete Duplicate Rows using Group By and Having Clause According to Delete Duplicate Rows in SQL, for finding duplicate rows, you need to use the SQL GROUP BY clause. The COUNT function can be used to verify the occurrence of a row using the Group by clause, which groups data according to the given columns.
To remove the duplicate columns we use the DISTINCT operator in the SELECT statement as follows: Syntax: SELECT DISTINCT column1, column2, ...
I have a more effective solution:
DELETE FROM MyTable
WHERE A NOT IN
(SELECT MIN(A)
FROM MyTable GROUP BY B, C, D
);
Attention: this works if "A" is not NULL. So, for some similar tasks it won't help.
Try:
select count(A) Occurrence, B, C, D from TableName group by B, C, D having count(A) > 1
To get the IDs of the duplicated Columns use:
select A from TableName where (B + ' ' + C + ' ' + D) in
(select B + ' ' + C + ' ' + D from TableName group by B, C, D having count(A) > 1)
select MyTable.A
from MyTable
join (select B,C,D
from MyTable
group by B,C,D
having Count(*)>1) as T2 on MyTable.B = T2.B
and MyTable.C = T2.C
and MyTable.D = T2.D
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With