站長資訊網(wǎng)
        最全最豐富的資訊網(wǎng)站

        歸納整理oracle數(shù)據(jù)庫去除重復(fù)數(shù)據(jù)常用的方法

        本篇文章給大家?guī)砹岁P(guān)于Oracle的相關(guān)知識,其中主要介紹了關(guān)于數(shù)據(jù)清理的時候常常會清除表中的重復(fù)的數(shù)據(jù),那么在oracle中怎么處理呢?下面一起來看一下,希望對大家有幫助。

        歸納整理oracle數(shù)據(jù)庫去除重復(fù)數(shù)據(jù)常用的方法

        推薦教程:《Oracle視頻教程》

        創(chuàng)建測試數(shù)據(jù)

        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_1 COL_2 COL_3
        1 2 3
        1 2 3
        5 2 3
        10 20 30

        針對指定列,查出去重后的結(jié)果集

        distinct

        select distinct t1.* from nayi224_180824 t1;
        COL_1 COL_2 COL_3
        10 20 30
        1 2 3
        5 2 3

        方法局限性很大,因為它只能對全部查詢的列做去重。如果我想對col_2,col3去重,那我的結(jié)果集中就只能有col_2,col_3列,而不能有col_1列。

        select distinct t1.col_2, col_3 from nayi224_180824 t1
        COL_2 COL_3
        2 3
        20 30

        不過它也是最簡單易懂的寫法。

        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_1 COL_2 COL_3 RN
        1 2 3 1
        10 20 30 1

        寫法上要麻煩不少,但是有更大的靈活性。

        針對指定列,查出所有重復(fù)的行

        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_1 COL_2 COL_3
        1 2 3
        1 2 3
        5 2 3

        要查兩次表,效率會比較低。不推薦。

        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_1 COL_2 COL_3 RN
        1 2 3 3
        1 2 3 3
        5 2 3 3

        只需要查一次表,推薦。

        刪除所有重復(fù)的行

        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);

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

        刪除重復(fù)數(shù)據(jù)并保留一條

        分析函數(shù)法

        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);

        擁有分析函數(shù)一貫的靈活性高的特點。可以為所欲為的分組,并通過改變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視頻教程》

        贊(0)
        分享到: 更多 (0)
        網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號
        主站蜘蛛池模板: 国产精品免费久久| 人妻少妇精品系列| 亚洲欧美国产精品专区久久| 老司机91精品网站在线观看| 日韩精品极品视频在线观看免费| 国产内地精品毛片视频| 国产成人精品日本亚洲直接| 无码精品日韩中文字幕| 免费精品久久久久久中文字幕| 亚洲国产精品一区| 国产精品天天看天天狠| 无码国内精品人妻少妇蜜桃视频| 鲸鱼传媒绿头鱼实验室之炮机测评日韩精品一级毛 | 99久久99久久精品国产片果冻 | 欧美亚洲综合免费精品高清在线观看| 亚洲人成亚洲精品| 国产精品欧美亚洲韩国日本| 国产伦精品免编号公布| 永久免费精品影视网站| 欧美精品亚洲精品日韩专区| 久久e热在这里只有国产中文精品99| 99国产精品国产免费观看| 亚洲国产精品热久久| 奇米精品一区二区三区在线观看| 国产精品成人免费福利| 99久久精品毛片免费播放| 久久国产精品99国产精| 久久99国内精品自在现线| 久久精品99久久香蕉国产色戒 | 国产高清精品一区| 2020最新久久久视精品爱| 91热成人精品国产免费| 91嫩草亚洲精品| 国产精品免费观看视频| 国产精品 91 第一页| 国产三级精品三级在线观看| 精品久久久无码中文字幕天天| 久久精品这里只有精99品| 欧美日韩国产成人高清视频,欧美日韩在线精品一 | 国产精品原创巨作av女教师| 精品深夜AV无码一区二区|