Question : Delete with subquery

I have the following script that shows me all duplicate records based on 4 fields.  How can I modify the query to delete the shown records.  I tried doing it as a subquery but no go.  How would you write this?

select * from EPI_CONFIG epi
Inner join (select Cfg_User, Cfg_Level, Cfg_Section, Cfg_Name, Cfg_Value from EPI_CONFIG
group by Cfg_User, Cfg_Level, Cfg_Section, Cfg_Name, Cfg_Value having count(*)>1) as dups
ON dups.Cfg_User = epi.Cfg_User AND
dups.Cfg_Level = epi.Cfg_Level AND
dups.Cfg_Section= epi.Cfg_Section AND
dups.Cfg_Name = epi.Cfg_Name AND
dups.Cfg_Value = epi.Cfg_Value

Answer : Delete with subquery

you cannot just delete like this.
it will delete all the rows  from your table where the subquery count is > 1.
not just the duplicate row.
you need to do something else.

one way is to create another table with the same structure
insert select distinct into that table all the columns that you use to check for dups

insert into temp_tbl
Select distinct Cfg_User, Cfg_Level, Cfg_Section, Cfg_Name
From EPI_CONFIG

update tmp
set the_rest_of_columns = ...
from temp_tbl a join original_tbl b on a.Cfg_User = b.... and  a.Cfg_Level = b.... and a.Cfg_Section = b.... and a.Cfg_Name = b....

then you drop the old table
and rename the new one with the old table name.

Another way is if you have some column that you can use to make distinction between the duplicate rows so you can add this condition to the delete statement, something like... if you have an autoincrement column, then for sure the auto_inc value for the first dup row is smaller than the auto_inc value for the second row.
or maybe you have a column that holds the row insert date or something.

delete a
from EPI_CONFIG a join EPI_CONFIG b on a.columnlist = b.columnlist
where a.the_distinct_column > b.the_distinct_column




Random Solutions  
 
programming4us programming4us