Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite how to remove all duplicated rows except one?

Tags:

sql

sqlite

I have a weird table call mytable, no column is primary.

  Name    |  Company  |  Position
 Michael     Google      Tester
 Michael     Google      Tester
 Michael     Google      Tester
 Peter       Facebook    Developer
 Peter       Facebook    Developer
 Peter       Facebook    Developer
 Peter       Facebook    Developer

What I want

  Name    |  Company  |  Position
 Michael     Google      Tester
 Peter       Facebook    Developer 

With some solutions I found with the same question here, they did not work. For example: DELETE FROM mytable WHERE Name NOT IN (SELECT MAX(Name) FROM mytable GROUP BY Company);

I should edit right in this table, use SQLite, no new table creation and no CTE. How can I do it?

like image 520
gnase Avatar asked Jul 20 '17 12:07

gnase


People also ask

How do I exclude duplicate rows in SQL?

The SQL DISTINCT keyword, which we have already discussed is used in conjunction with the SELECT statement to eliminate all the duplicate records and by fetching only the unique records.

How do you delete duplicate records in SQL and keep one record in Oracle?

Once you've found the duplicate records in a table, you often want to delete the unwanted copies to keep your data clean. If a table has a few duplicate rows, you could do this manually one by one by using a simple DELETE statement.


2 Answers

You can choose to keep the min or max of rowid grouping by the 3 columns shown.

delete from myTable
where rowid not in (select min(rowid)
                    from myTable
                    group by name,company,position)
like image 160
Vamsi Prabhala Avatar answered Sep 22 '22 09:09

Vamsi Prabhala


SELECT DISTINCT  Name,  Company,  Position 
FROM yourTableName

In case you want to delete "duplicate" rows you might write this:

yourTableName - should be your real database table

yourColumn - should be your real database column

DELETE FROM yourTableName
WHERE yourColumn NOT IN (
    SELECT yourColumn 
    FROM yourTableName
    WHERE yourColumn IS NOT NULL
)
like image 22
Roxy'Pro Avatar answered Sep 23 '22 09:09

Roxy'Pro