0

So I have a database with more than 2000 line and I wanted to delete the duplicate value based on 2 rows.

For Example :

no     server     name
1      serv1       a
2      serv1       b
3      serv1       b
4      serv1       b
5      serv2       a
6      serv2       b
7      serv2       c
8      serv2       c

So basically I wanted to remove the duplicate IF two of the rows have a duplicate
BUT I dont want to remove them if just one of the row has duplicate.

Expected Output:

no      server     name
1       serv1       a
2       serv1       b
3       serv2       a
4       serv2       b
5       serv2       c

Any answer would be appreciated.

Ron
  • 3
  • 1
  • 4

3 Answers3

1

Use select distinct:

select distinct server, name
from t;

If you have a lot of duplicates, the simplest way is probably to recreate the table:

create table temp_t as
    select min(no) as no, server, name
    from t
    group by server, name;

truncate table t;

insert into t (no, server, name)
    select no, server, name
    from temp_t;

If you don't have many rows, then delete is fine:

delete t
    from t join
         (select server, name, min(no) as min_no
          from t
          group by server, name
         ) tt
         on t.server = tt.server and t.name = tt.name
    where t.no > tt.min_no;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • Hey, @Gordon Linoff.do you know a way how to delete the duplicate data from the database too? – Ron Sep 08 '19 at 03:47
1

There is many ways to do what you want. If you 're looking for just SELECTing the data without duplicates then you could use:

  • DISTINCT

    SELECT DISTINCT Server,
                    Name
    FROM YourTableName
    
  • GROUP BY

    SELECT Server,
           Name
    FROM YourTableName
    GROUP BY Server, Name
    
  • Window function (ROW_NUMBER) in a subquery

    SELECT Server, 
           Name
    FROM
    (
      SELECT *,
             ROW_NUMBER() OVER(PARTITION BY Server, Name ORDER BY Server) RN
      FROM YourTableName
    ) TT
    WHERE TT.RN = 1
    

You delete the duplicates as

DELETE T1
FROM
(
  SELECT ROW_NUMBER() OVER(PARTITION BY Server, Name ORDER BY Server) RN
  FROM T --YourTableName
) T1 JOIN
(
  SELECT ROW_NUMBER() OVER(PARTITION BY Server, Name ORDER BY Server) RN
  FROM T --YourTableName
) T2 ON T1.RN = T2.RN
WHERE T1.RN > 1;
Ilyes
  • 14,640
  • 4
  • 29
  • 55
0

You can use the following query to remove duplicates

DELETE t1 FROM tablename t1 INNER JOIN tablename t2 
WHERE t1.id < t2.id AND t1.server = t2.server AND t1.name = t2.name;
Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20