Having this table: locs
o_id | action | second_action | user | entity
1 | Changed site from '12' to '13'. | Changed site from '12' to '13'. | admin | 1111
2 | Changed site from '12' to '13'. | Changed site from '12' to '13'. | admin | 1111
3 | Changed site from '12' to '13'. | Changed site from '12' to '13'. | admin | 1111
4 | Changed panel from 'st' to 'bl'. | Changed panel from 'st' to 'bl'. | john | 2222
5 | Changed panel from 'st' to 'bl'. | Changed panel from 'st' to 'bl'. | john | 2222
6 | Changed panel from 'nr' to 'bl'. | Changed panel from 'nr' to 'bl'. | admin | 3333
I want to delete entries that are duplicated base on the action, second_action and entity column so this will be :
locs
o_id | action | second_action | user | entity
1 | Changed site from '12' to '13'. | Changed site from '12' to '13'. | admin | 1111
4 | Changed panel from 'st' to 'bl'. | Changed panel from 'st' to 'bl'. | john | 2222
6 | Changed panel from 'nr' to 'bl'. | Changed panel from 'nr' to 'bl'. | admin | 3333
Because I have a older PostgreSQL I can't use common table expression described here: https://www.sqlservertutorial.net/sql-server-basics/delete-duplicates-sql-server/
So I went with a more classic SQL:
DELETE FROM locs
WHERE o_id NOT IN
(SELECT * FROM (SELECT MIN(o_id)
FROM locs
GROUP BY action, second_action, entity) AS t);
I had to use SELECT * FROM AS t because of some weird SQL error:
[HY000][1093] You can't specify target table 'locs' for update in FROM clause
Is my SQL approach correct ? Is there any better query for this ? I will run that on a bigger data set and I wanted to be sure that will do what I'm expecting.