ruby - Remove duplicated records based on pair of columns? -
i have table folowwing columns:
id app_id tag_id created_at updated_at
the pair app_id
, tag_id
should uniq. how remove oldest duplicated records based on updated_at
column determine "oldest" , pair (app_id, tag_id)
determine "duplicated"?
activerecord::base.connection.execute <<-sql delete my_table id in ( select t1.id my_table t1 inner join ( select app_id, tag_id, min(updated_at) oldest my_table group app_id, tag_id ) t2 on t1.app_id = t2.app_id , t1.tag_id = t2.tag_id , t1.updated_at = t2.oldest ) sql
replace my_table
actual table name.
Comments
Post a Comment