How to display duplicate rows / records ?
How to delete duplicate rows / records ?
Consider a table ( table_a ) as below,
create table table_a
(
col_1 int --unique key
duplicateColumn1 varchar(10), --column for which we want to check duplicates
duplciateColumn1 varchar(20) --another column for which we want to check duplicates
)
--# How to select duplicate rows.
select A.* from table_a A
group by duplicateColumn1, duplicateColumn2
having count(*) > 1
--# How to delete duplicate rows, keeping only one copy of the row.
delete table_a
from table_a A, table_a B
where
A.duplicateColumn1 = B.duplicateColumn1
AND A.duplicateColumn2 = B.duplicateColumn2
AND A.col_1 > B.col_1
or you can use SQL below,
delete table_a
where col_1 not in
( select max( col_1 ) from table_a group by duplicateColumn1 , duplicateColumn2 )
--# How to delete duplicate rows, when table does not have a primary / unique key.
You can add an identity column to the table and treat it as an unique key. And follow above SQL.
Or select table data into a temp table with identity column, follow above SQL on temp table, then" refresh" table_a data with the temp table data.
Saturday, May 24
SQL to delete duplicate rows / records
Labels:
SQL Tricks
Subscribe to:
Post Comments (Atom)

0 comments:
Post a Comment