测试去除重复数据

1、创建测试表,初始化数据
JZH@jzh>create table t (x number,y number);
Table created.
JZH@jzh>insert into t values (1,1);
1 row created.
JZH@jzh>insert into t values (1,1);
1 row created.
JZH@jzh>insert into t values (2,2);
1 row created.
JZH@jzh>commit;
Commit complete.
JZH@jzh>select * from t;
         X          Y
---------- ----------
         1          1
         1          1
         2          2
2、使用distinct方法
JZH@jzh>select distinct x,y from t;
         X          Y
---------- ----------
         1          1
         2          2
3、使用group by方法
JZH@jzh>select x,y from t where (x,y) in (select x,y from t group by x,y having count(*)>1);
         X          Y
---------- ----------
         1          1
         1          1
4、使用rowid方法
1)查重
JZH@jzh>JZH@jzh>select * from t a where a.rowid <> (select max(rowid) from t b where a.x=b.x and a.y=b.y);
         X          Y
---------- ----------
         1          1
JZH@jzh>select * from t a where a.rowid <> (select min(rowid) from t b where a.x=b.x and a.y=b.y);
         X          Y
---------- ----------
         1          1
2)去重
JZH@jzh>delete from t a where a.rowid<>(select max(rowid) from t b where a.x=b.x and a.y=b.y);
1 row deleted.
JZH@jzh>commit;
Commit complete.
JZH@jzh>select * from t;
         X          Y
---------- ----------
         1          1
         2          2
5、使用分析函数方法
1)查重
JZH@jzh>select a.x,a.y,row_number () over (partition by a.x,a.y order by a.rowid) rn from t a;
         X          Y         RN
---------- ---------- ----------
         1          1          1
         1          1          2
         2          2          1
JZH@jzh>select b.x,b.y from (select a.x,a.y,row_number () over (partition by a.x,a.y order by a.rowid) rn from t a) b where b.rn>1;
         X          Y
---------- ----------
         1          1
2)去重
JZH@jzh>delete from t where rowid in (select rowid from (select a.x,a.y,row_number () over (partition by a.x,a.y order by a.rowid) rn from t a) b where rn>1);
1 row deleted.
JZH@jzh>select * from t;
         X          Y
---------- ----------
         1          1
         2          2






来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10271187/viewspace-1719676/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10271187/viewspace-1719676/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值