• 技术文章 >数据库 >Oracle

    归纳整理oracle数据库去除重复数据常用的方法

    WBOYWBOY2022-08-22 17:59:30转载1073
    本篇文章给大家带来了关于Oracle的相关知识,其中主要介绍了关于数据清理的时候常常会清除表中的重复的数据,那么在oracle中怎么处理呢?下面一起来看一下,希望对大家有帮助。

    php入门到就业线上直播课:进入学习

    推荐教程:《Oracle视频教程

    创建测试数据

    create table nayi224_180824(col_1 varchar2(10), col_2 varchar2(10), col_3 varchar2(10));
    insert into nayi224_180824
    select 1, 2, 3 from dual union all
    select 1, 2, 3 from dual union all
    select 5, 2, 3 from dual union all
    select 10, 20, 30 from dual ;
    commit;
    select*from nayi224_180824;
    COL_1COL_2COL_3
    123
    123
    523
    102030

    针对指定列,查出去重后的结果集

    distinct

    select distinct t1.* from nayi224_180824 t1;
    COL_1COL_2COL_3
    102030
    123
    523

    方法局限性很大,因为它只能对全部查询的列做去重。如果我想对col_2,col3去重,那我的结果集中就只能有col_2,col_3列,而不能有col_1列。

    select distinct t1.col_2, col_3 from nayi224_180824 t1
    COL_2COL_3
    23
    2030

    不过它也是最简单易懂的写法。

    row_number()

    select *
      from (select t1.*,
                   row_number() over(partition by t1.col_2, t1.col_3 order by 1) rn
              from nayi224_180824 t1) t1
     where t1.rn = 1
    ;
    COL_1COL_2COL_3RN
    1231
    1020301

    写法上要麻烦不少,但是有更大的灵活性。

    针对指定列,查出所有重复的行

    count having

    select *
      from nayi224_180824 t
     where (t.col_2, t.col_3) in (select t1.col_2, t1.col_3
                                    from nayi224_180824 t1
                                   group by t1.col_2, t1.col_3
                                  having count(1) > 1)
    COL_1COL_2COL_3
    123
    123
    523

    要查两次表,效率会比较低。不推荐。

    count over

    select *
      from (select t1.*,
                   count(1) over(partition by t1.col_2, t1.col_3) rn
              from nayi224_180824 t1) t1
     where t1.rn > 1
    ;
    COL_1COL_2COL_3RN
    1233
    1233
    5233

    只需要查一次表,推荐。

    删除所有重复的行

    delete from nayi224_180824 t
     where t.rowid in (
                       select rid
                         from (select t1.rowid rid,
                                       count(1) over(partition by t1.col_2, t1.col_3) rn
                                  from nayi224_180824 t1) t1
                        where t1.rn > 1);

    就是上面的语句稍作修改。

    删除重复数据并保留一条

    分析函数法

    delete from nayi224_180824 t
     where t.rowid in (select rid
                         from (select t1.rowid rid,
                                      row_number() over(partition by t1.col_2, t1.col_3 order by 1) rn
                                 from nayi224_180824 t1) t1
                        where t1.rn > 1);

    拥有分析函数一贯的灵活性高的特点。可以为所欲为的分组,并通过改变orderby从句来达到像”保留最大id“这样的要求。

    group by

    delete from nayi224_180824 t
     where t.rowid not in
           (select max(rowid) from nayi224_180824 t1 group by t1.col_2, t1.col_3);

    牺牲了一部分灵活性,换来了更高的效率。

    推荐教程:《Oracle视频教程

    以上就是归纳整理oracle数据库去除重复数据常用的方法的详细内容,更多请关注php中文网其它相关文章!

    声明:本文转载于:脚本之家,如有侵犯,请联系admin@php.cn删除

    前端(VUE)零基础到就业课程:点击学习

    清晰的学习路线+老师随时辅导答疑

    自己动手写 PHP MVC 框架:点击学习

    快速了解MVC架构、了解框架底层运行原理

    专题推荐:oracle
    上一篇:图文详解Oracle锁表解决方法的详细记录 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • ❤️‍🔥共22门课程,总价3725元,会员免费学• ❤️‍🔥接口自动化测试不想写代码?• 详解Oracle控制文件及日志文件的管理问题• Oracle查看表空间使用率以及实例解决爆满问题• plsql和oracle的区别是什么• oracle中connect by怎么用• oracle怎么解决错误1053
    1/1

    PHP中文网