0

I'm using an Access Database and have the following question:

How is it possible to remove entire rows based on certain columns?

For example I have the following table:

Id | Name |   Desc  | Contact No. | 
1  | XYZ  | bla bla | 123456      | 
2  | ABC  | bla bla | 123456      |
3  | TUV  | hmmmmm  | 123456      |
4  | XYZ  | bla bla | 123456      |

I want to remove all lines of data which have duplicate information, so in this case, rows 1 & 4. The only issue of course, is the Id, which makes the overall row unique....

How would I go about removing this? Is there some SQL code that can handle this?

LittleBobbyTables - Au Revoir
  • 32,008
  • 25
  • 109
  • 114
user559142
  • 12,279
  • 49
  • 116
  • 179

2 Answers2

2

An autojoin for detecting the duplicate:

DELETE FROM Table
WHERE Id IN
(
    SELECT t1.Id
    FROM Table t1
        INNER JOIN join Table t2
            ON t1.Name = t2.Name
            AND t1.[Desc] = t2.[Desc]
            AND t1.Contact = t2.Contact 
)

If you want to keep only one row for each duplicate, look this question : Deleting duplicate rows from a table

DELETE FROM Table
WHERE Id NOT IN
(
    SELECT MIN(Id)
    FROM Table
    GROUP BY Name, [Desc], Contact
)
Community
  • 1
  • 1
Cyril Gandon
  • 16,830
  • 14
  • 78
  • 122
2

If you need to delete all rows that have a duplicate, you could use this Ms-Access SQL query:

DELETE tbl.Id
FROM tbl
WHERE (((tbl.Id) In (SELECT tbl.Id
  FROM
    (SELECT [Name], [Desc], [Contact]
     FROM tbl
     GROUP BY [Name], [Desc], [Contact]
     HAVING COUNT(*)>1)  AS [t1]
  INNER JOIN tbl
  ON ([t1].Contact = tbl.Contact)
     AND ([t1].[Desc] = tbl.[Desc])
     AND ([t1].[Name] = tbl.[Name]))));

To keep just the first duplicate you could use this:

DELETE tbl.Id
FROM tbl
WHERE (tbl.Id) Not In (SELECT Min(tbl.Id) AS MinId
FROM tbl
GROUP BY tbl.[Name], tbl.[Desc], tbl.[Contact]);
fthiella
  • 48,073
  • 15
  • 90
  • 106