表结构如下:
create table TMP_1 ( ID VARCHAR2(20), SAL NUMBER ) |
查询重复记录:
SELECT * FROM TMP_1 A WHERE ROWID != (SELECT MAX(ROWID) FROM TMP_1 B WHERE A.ID = B.ID AND A.SAL = B.SAL); |
删除重复记录(只能删除完全相同的行记录):
DELETE FROM TMP_1 A WHERE ROWID != (SELECT MAX(ROWID) FROM TMP_1 B WHERE A.ID = B.ID AND A.SAL = B.SAL); |
删除重复记录(可以删除针对某个字段相同的记录,结果只保留一行):
DELETE FROM TMP_1 WHERE ROWID IN (SELECT ROWID FROM (SELECT ROWID, ROW_NUMBER() OVER(PARTITION BY sal ORDER BY sal) RN FROM tmp_1) WHERE RN > 1); |
ROW_NUMBER() :表示根据sal 分组,在分组内部根据 sal 排序
而这个值就表示每组内部排序后的顺序编号(组内连续的唯一的)