I have a table with a column serial_number that is repeated a few times. How would I delete the entire row except the first duplicate?
By the following, I can select all the duplicates. But can't delete.
SELECT serial_number, COUNT(*) FROM trademark_merge GROUP BY serial_number HAVING COUNT(*) > 1
Assuming that the primary key of your table is id, you could phrase this as a delete/join query, like:
delete tm
from trademark_merge tm
inner join (
select serial_number, min(id) id
from trademark_merge
group by serial_number
) tm1 on tm.serial_number = tm1.serial_number and tm.id > tm1.id
Related
I have a table, TABLE1, with two columns: SSN and Date.
I have another table, TABLE2, with SSN, name and Surname.
I want to view the Name and the Surname of the person who is associated to the SSN that appears the highest number of times in the first table (the one with SSN and Date)
Which query should I write?
The fastest way is probably to aggregation before joining:
select *
from (select t1.ssn
from table1 t1
group by t1.ssn
order by count(*) desc
limit 1
) t1 left join
table2 t2
using (ssn)
Try this.
SELECT t1.ssn, t2.name, t2.surname, count(*) as vol
FROM table1 t1
LEFT JOIN table2 t1 on t1.ssn = t2.ssn
GROUP BY 1
ORDER BY count(*) DESC
LIMIT 1
Use a subquery in the WHERE clause which returns the SSN that appears the most in table1:
SELECT *
FROM table2
WHERE SSN = (SELECT SSN FROM table1 GROUP BY SSN ORDER BY COUNT(*) DESC LIMIT 1)
With an index on SSN (which I assume exists) this is the fastest way to get the result.
I have a table like given below name recomendation
I want to delete all the rows where cnt has the minimum value and there exist multiple records of ID_recipient.
If there is a single record of ID_recipient it shouldn't get deleted whatever the cnt value may be.
The ones highlighted in blue are the records that must stay.
I tried:
DELETE from table where(
SELECT DISTINCT(A.ID_recipient), DISTINCT(A.cnt) FROM (
SELECT MIN(cnt) as cnt FROM recomendation_table_ID_recipient GROUP BY ID_recipient HAVING COUNT(*) > 1 ) as A);
which is not working.
If you want to use 2 dimensions you have to use IN clause.
Your subquerys doesn't make much sense, so you should test this first, or post data with wanted example
DELETE from recomendation_table_ID_recipient where (ID_recipient,cnt) IN (
SELECT DISTINCT A.ID_recipient, A.cnt FROM (
SELECT ID_recipient, MIN(cnt) as cnt FROM recomendation_table_ID_recipient GROUP BY ID_recipient HAVING COUNT(*) > 1 ) as A);
delete t1 from recomendation_table_ID_recipient t1 join (
select ID_recipient, min(cnt) as cnt from recomendation_table_ID_recipient
group by ID_recipient
having count(*) > 1
) t2 on t1.ID_recipient = t2.ID_recipient and t1.cnt = t2.cnt;
See db-fiddle
I have a query in MySQL based on which I am finding duplicate records of some columns.
select max(id), count(*) as cnt
from table group by start_id, end_id, mysqltable
having cnt>1;
This above query gives me the max(id) and the count of number of records that have start_id,end_id,mysqltable column values same.
I want to delete all the records that match the max(id) column of the above query
How can I do that?
I have tried like below
delete from table
where (select max(id), count(*) as cnt
from table group by start_id,end_id,mysqltable
having cnt>1)
But Unable to delete records
You can remove duplicate records using JOIN.
DELETE t1 FROM table t1
INNER JOIN
table t2
WHERE
t1.id > t2.id AND t1.start_id = t2.start_id AND t1.end_id = t2.end_id AND t1.mysqltable = t2.mysqltable;
This query keeps the lowest id and remove the highest.
I think so this command should work:
delete from table
where id in
( select max(id) from table
group by start_id, end_id, mysqltable
having count(*) > 1
);
I have inherited a database that has a "Duplicate problem".
when I run:
SELECT myFIELD, COUNT(*) c FROM myTABLE GROUP BY myFIELD HAVING c > 1;
I get ~600 records that are duplicated
None or tripled or any other multiple
I wish to kill off one of the records, leaving me with no duplicates.
What does the query look like?
You can use a query like this ... you should repete the query depending how many time the same row is duplicated ..
delete from my_table
where (myFIELD, id) in
(select a.myFIELD, max(a.id)
FROM myTABLE as a GROUP BY a.myFIELD HAVING count(*) > 1)
otherwise you can use
delete from my_table
where (myFIELD, id) not in
(select a.myFIELD, min(a.id)
FROM myTABLE as a GROUP BY a.myFIELD )
this should delete all the duplicated rows in a shot
If there problem with table name
delete from my_table
where (myFIELD, id) in (select field, id from
(select a.myFIELD as field, max(a.id) as id
FROM myTABLE as a GROUP BY a.myFIELD HAVING count(*) > 1) as t)
I have a database table with three columns. Id, user_id, book_id. In this table, there are some duplicates. a user_id should only have one record of a book_id, but in some cases, a user_id has several book_ids. There are a couple of million records already and I'm wondering how to remove any duplicates.
Try following.
SQL SERVER
WITH ORDERED AS
(
SELECT id
ROW_NUMBER() OVER (PARTITION BY [user_id] , [book_id] ORDER BY id ASC) AS rn
FROM
tableName
)
delete from tableName
where id in ( select id from ORDERED where rn != 1)
MYSQL
delete from tableName
where id not in(
select MIN(id)from tableName
group by user_id, book_id
)
Edited as per comments - In MySQL, you can't modify the same table which you use in the SELECT part
This will solve the issue.
delete from tableName
where id not in(
select temp.temp_id from (
select MIN(id) as temp_id from tableName
group by user_id, book_id
) as temp
)
This will keep only one combination of (user_id, book_id)
If you execute this statement below, it will delete all duplicate records of user_ID and leaving only the greatest ID for each user_ID
DELETE a
FROM tableName a
LEFT JOIN
(
SELECT user_ID, MAX(ID) max_ID
FROM tableName
GROUP BY user_ID
) b ON a.user_ID = b.user_ID AND
a.ID = b.max_ID
WHERE b.max_ID IS NULL
SQLFiddle Demo
Hope this query will allow you to remove duplicates:
DELETE bl1 FROM book_log bl1
JOIN book_log bl2
ON (
bl1.id > bl2.id AND
bl1.user_id = bl2.user_id AND
bl1.book_id = bl2.book_id
);
Demo