标签:sql
举出4种去重的方法:
第一种:distinct
SQL> create table t as select * from dba_objects; Table created. SQL> insert into t select * from t; 87401 rows created. SQL> commit; Commit complete. SQL> create table tmp_t1 as select distinct * from t; Table created. SQL> drop table t; Table dropped. SQL> select count(*) from t; COUNT(*) ---------- 87401
第二种,用rowid
DELETE FROM t WHERE ROWID <> (SELECT MIN(ROWID) FROM t b WHERE b.object_id = t.object_id 5 AND b.object_name = t.object_name); 174802 rows deleted. SQL> select count(*) from t; COUNT(*) ---------- 87401
第三种,用rowid + group by 的方法:
SQL> insert into t select * from t where rownum<100; 99 rows created. Elapsed: 00:00:00.21 SQL> commit; Commit complete. Elapsed: 00:00:00.03 DELETE FROM t WHERE ROWID NOT IN 2 3 (SELECT MIN(ROWID) FROM t GROUP BY object_id, object_name); 99 rows deleted. Elapsed: 00:00:00.41
DELETE FROM t WHERE NOT EXISTS (SELECT 1 FROM (SELECT MIN(ROWID) rid FROM t GROUP BY object_id, object_name) b 5 WHERE b.rid = t.rowid); 99 rows deleted. Elapsed: 00:00:01.32
---第四种, 用分析函数
DELETE FROM t WHERE ROWID IN (SELECT b.rd FROM (SELECT ROWID rd, row_number() over(PARTITION BY object_id, object_name ORDER BY object_id) rn FROM t) b 6 WHERE b.rn > 1); 99 rows deleted. Elapsed: 00:00:00.50
标签:sql
原文地址:http://7642644.blog.51cto.com/7632644/1678559