Data Warehousing Community Forum
February 10, 2012, 12:40:07 am

Pages: [1]   Go Down
  Print  
Author Topic: Deletion of Duplicate records from a table (SQL)?  (Read 1099 times)
Whoever
Administrator
*****

Reputation: +34/-0
Offline Offline

Posts: 124



View Profile WWW
« on: June 13, 2007, 12:07:54 pm »

Hi

I have a table that has duplicate records but still I am unable to identify them with a unique field.

Can anybody help me in finding the duplicate records and how to discard them from the table?

Note : I need the Microsoft SQL Query not related to Oracle.
Logged

If most people said what’s on their minds, they’d be speechless.
Arvind
DW Apprentice
**

Reputation: +10/-0
Offline Offline

Posts: 78


View Profile
« Reply #1 on: July 19, 2007, 07:30:44 pm »

As I know, there are 3 methods to remove the Duplicate records in a table in MS-SQL.

I'll be back with all those three types.
Logged

regards,
Arvind
NoName
DW Fresher
*

Reputation: +1/-0
Offline Offline

Posts: 1


View Profile
« Reply #2 on: March 21, 2008, 02:57:01 pm »

Table Structure is

Sno     Sal
1        1000
2        4000
3        2000
4        3000
1        1000
2        4000
1        1000
2        4000
4        3000
3        2000
2        4000

Approach - 1
WITH DUPLICATEROW (ROW, GROUPROW,SNO,SAL)
AS
(
SELECT ROW,
          GROUPROW = CASE WHEN SNO=B.SNO
                                  THEN
                                         (SELECT COUNT(*) FROM (SELECT ROW_NUMBER() OVER (ORDER BY SNO) AS ROW,
                                                                                         SNO,SAL FROM TEMP1
                                                                                         ) AS A
                                  WHERE
                                                A.SNO=B.SNO AND
                                                A.ROW<=B.ROW)
                                  END,
          SNO,
          SAL
FROM
       (SELECT
                     ROW_NUMBER() OVER (ORDER BY SNO) AS ROW,
                     SNO,
                     SAL
        FROM
                     TEMP1
       )AS B
)
DELETE FROM DUPLICATEROW WHERE GROUPROW<>1

Approach – 2 ( Three step approach)
SELECT DISTINCT * INTO #DUPES FROM TEMP1

DELETE FROM TEMP1 WHERE SNO IN (SELECT SNO FROM TEMP1 GROUP BY SNO HAVING COUNT(*) >=2)

INSERT TEMP1
       SELECT * FROM #DUPES
Logged
herbert11
DW Apprentice
**

Reputation: +4/-0
Offline Offline

Posts: 25


View Profile
« Reply #3 on: September 07, 2011, 12:34:04 pm »

There are various methods to identify the duplicate records in any database table. We can identify the duplicate entries by retrieving the number of records of any data in the table. It can be done using COUNT function. It will let you know the entries that are entered more than one and can be identified easily and can be removed. The other way in my view is to use DISTINCT to view the table records without duplicate entries. It will give you the results after removing the duplicate records.
Logged
Pages: [1]   Go Up
  Print  
 
Jump to: