1

I have written a query delete from Table1 where Tableid in(select Tableid from Table1 group by Tableid having count(*) >1) but this query removes all the data having count greater than 1.

Can someone help me with a single line query that deletes the duplicate data and resetting the count to 1.

I have table Table1 with

Tableid Count 

1        10

2        2

3        1

4        NULL

5        31

Post Delete it should be

Tableid Count 
1        1

2        1

3        1

4        NULL

5        1

User008
  • 377
  • 3
  • 18

6 Answers6

1

To delete all the duplicate data: Group the column that may have the same data.

DELETE FROM table 
WHERE id IN (SELECT id FROM table GROUP BY column HAVING COUNT(column) > 1)

To delete the duplicate and keep one of it: Get at least (1) data from the duplicate and grouped column.

DELETE t1 FROM table t1, table t2 
WHERE t1.id <> t2.id AND t1.column = t2.column

Back-up your data first before testing anything.

rhavendc
  • 985
  • 8
  • 21
1

I think this is what you are looking for

DECLARE @Table TABLE 
(   
    Name VARCHAR(20),
    Value INT 
);

;WITH T AS (
    SELECT CONCAT('a',1) AS Name, 1 AS Value 
    UNION ALL
    SELECT CONCAT('a',T.Value + 1) AS Name, T.Value + 1 FROM T
    WHERE T.Value < 5
)
INSERT INTO @Table 
SELECT T.Name ,
       T.Value 
FROM T


INSERT INTO @Table
        ( Name, Value )
VALUES  ( 'a5', -- Name - varchar(20)
          5  -- Value - int
          ),( 'a5', -- Name - varchar(20)
          5  -- Value - int
          )

INSERT INTO @Table
SELECT * FROM @Table

INSERT INTO @Table
SELECT * FROM @Table

SELECT 
    COUNT(*) AS TotalCount
    , Name 
    , Value 
FROM 
    @Table 
GROUP BY 
    Name ,
    Value 
ORDER BY 
    Name



DELETE T 
FROM (
    SELECT 
        Name 
        , Value
        , ROW_NUMBER() OVER(PARTITION BY Name, Value ORDER BY Value) AS RN
    FROM 
        @Table
) AS T
WHERE T.RN > 1


SELECT COUNT(*) AS TotalCount, Name, Value 
FROM @Table 
GROUP BY Name, Value
ORDER BY Name, Value
0
  1. Sort the data alphabetically or numerical order.
  2. Check if current field is equal to previous field.
  3. Delete if the same.
Captain Catz
  • 96
  • 1
  • 12
0

i would suggest to use select query here instead of delete

select tableId,case when count >1 then (count-(count-1)) 
                    else count
               end as count
from table 1;
Ankit Agrawal
  • 2,426
  • 1
  • 13
  • 27
0

try this

 DELETE FROM Table1 
       WHERE Tableid IN (SELECT Tableid FROM Table1 GROUP BY Tableid HAVING COUNT(*) > 1)
drneel
  • 2,887
  • 5
  • 30
  • 48
JYoThI
  • 11,977
  • 1
  • 11
  • 26
0

You can also set a uniquie key on the field where are make the row unique with the KEYWORD IGNORE.

It will vreate a unique key and deletes direct the duplications.

ALTER IGNORE TABLE mytable
ADD UNIQUE KEY (Tableid);
Bernd Buffen
  • 14,525
  • 2
  • 24
  • 39