一个简单的事情是这样的
delete /*+RULE*/ from t
where rowid in ( select rid
from ( select rowid rid,
row_number() over
(partition by cust_seg_nbr order by rowid) rn
from t
)
where rn <> 1 );
但是,如果您有大量数据,那么
检查此链接http://www.rampant-books.com/t_stoever_delete_duplicates.htm或使用下面的代码
DECLARE -- Code ©2004 by Edward Stoever
CURSOR c_get_duplicates
IS
SELECT ssrfees_term_code, ssrfees_crn, ssrfees_detl_code,
ssrfees_ftyp_code, ssrfees_levl_code, COUNT (*)
FROM ssrfees
HAVING COUNT (*) > 1
GROUP BY ssrfees_term_code,
ssrfees_crn,
ssrfees_detl_code,
ssrfees_ftyp_code,
ssrfees_levl_code;
var_get_duplicates c_get_duplicates%ROWTYPE;
CURSOR c_del_only_one
IS
SELECT ROWID
FROM ssrfees
WHERE ssrfees_term_code = var_get_duplicates.ssrfees_term_code
AND ssrfees_crn = var_get_duplicates.ssrfees_crn
AND ssrfees_detl_code = var_get_duplicates.ssrfees_detl_code
AND NVL(ssrfees_ftyp_code,'1') = NVL(var_get_duplicates.ssrfees_ftyp_code,'1')
AND NVL(ssrfees_levl_code,'1') = NVL(var_get_duplicates.ssrfees_levl_code,'1');
var_del_only_one ROWID;
BEGIN
OPEN c_get_duplicates;
LOOP
FETCH c_get_duplicates
INTO var_get_duplicates;
EXIT WHEN c_get_duplicates%NOTFOUND;
OPEN c_del_only_one;
FETCH c_del_only_one
INTO var_del_only_one;
DELETE FROM ssrfees
WHERE ROWID = var_del_only_one;
COMMIT;
CLOSE c_del_only_one;
END LOOP;
CLOSE c_get_duplicates;
END;
/