I created a MySQL table and that has been filling up in the last couple of weeks. Now I found out that some rows contains duplicates
The fields deviceId, counter1, counter2
together are a unique index. However I cannot ALTER the Table as there are duplicates. So my quesiton is how to remove these duplicates using a MySQL query
Entries have duplicates on counter1
and / or counter2
*deviceId ts counter1 counter2*
device001 2018-06-11 10:18:51 1 2
device001 2018-06-11 10:23:10 1 2
device002 2018-06-12 08:27:33 321 20
device002 2018-06-12 09:49:13 321 20
And from these duplicates 1 should be removed. Any suggestions on how to do this simple? I have tried the following:
Created a copy of the Table
CREATE TABLE sp_data_copy LIKE sp_data_orig;
INSERT sp_data_copy SELECT * FROM sp_data_orig;
Select and delete from copy Table
DELETE FROM sp_data_copy
WHERE ts IN (
SELECT ts
FROM sp_data_orig
GROUP BY counter1, counter2
HAVING COUNT(*) > 1);
But obviously it has not worked for me. Hope you have some suggestions. Thanks in advance