1

I'm working with SQLite and currently attempting to delete certain duplicate rows from a certain user (with ID 12345). I've managed to identify all of the rows I wish to delete, but I'm now unsure how to go about deleting these rows. This is my SELECT query:

SELECT t.*
from (select t.*, count(*) over (partition by code, version) 
as cnt from t) t
where cnt >= 2 and userID = "12345";

How would I go about deleting the rows matching this result? Can I use the query above in some way to identify which rows I want to delete? Thanks in advance!

2 Answers 2

1

You can get all the duplicate code, version combinations for userid = '12345' with this query:

select code, version
from tablename
where userid = '12345'
group by code, version
having count(*) > 1

and you can use it in the DELETE statement:

delete from tablename 
where userid = '12345'
and (code, version) in (
  select code, version
  from tablename
  where userid = tablename.userid
  group by code, version
  having count(*) > 1
)

If you want to keep 1 of the duplicate rows, use MIN() window function to get the row with the min rowid for each combination of code, version and exclude it from being deleted:

delete from tablename 
where userid = '12345'
and rowid not in (
  select distinct min(rowid) over (partition by code, version)
  from tablename
  where userid = tablename.userid
)
Sign up to request clarification or add additional context in comments.

4 Comments

I see. Would it be possible to only delete one of the duplicate rows if the user 12345 has 2 rows with the same value?
Can this work if one of the duplicate rows is "owned" by another user than 12345? Or must both rows be "owned" by 12345?
I understand. What would I need to modify in order to have it working with rows "owned" by both 12345 and another user? Only deleting the row owned by 12345.
This is a different requirement for which you should post a new question where you must explain what you want.
0

Hmmm . . . you don't have to use window functions:

delete from t
    where exists (select 1
                  from t t2
                  where t2.code = t.code and t2.version = t.version
                  group by code, version
                  having count(*) >= 2
                 );

2 Comments

What is t2 in this case?
@Californium . . . It is a table alias for the reference to your (unnamed) table in the subquery. I note that this answer was 12 minutes earlier than the other answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.