站長資訊網
        最全最豐富的資訊網站

        歸納整理oracle數據庫去除重復數據常用的方法

        本篇文章給大家帶來了關于Oracle的相關知識,其中主要介紹了關于數據清理的時候常常會清除表中的重復的數據,那么在oracle中怎么處理呢?下面一起來看一下,希望對大家有幫助。

        歸納整理oracle數據庫去除重復數據常用的方法

        推薦教程:《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_1 COL_2 COL_3
        1 2 3
        1 2 3
        5 2 3
        10 20 30

        針對指定列,查出去重后的結果集

        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去重,那我的結果集中就只能有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

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

        針對指定列,查出所有重復的行

        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

        只需要查一次表,推薦。

        刪除所有重復的行

        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視頻教程》

        贊(0)
        分享到: 更多 (0)
        網站地圖   滬ICP備18035694號-2    滬公網安備31011702889846號
        主站蜘蛛池模板: 日产精品久久久久久久性色| 亚洲午夜精品久久久久久人妖| 99精品久久精品一区二区| 久久人人爽人人精品视频| 亚洲国产综合精品中文第一区| 久久99国产综合精品免费| 久99精品视频在线观看婷亚洲片国产一区一级在线 | 国产在线精品一区二区不卡| 人妻精品久久无码区| 亚洲午夜福利精品无码| 麻豆国产精品VA在线观看不卡| 国产精品一区二区三区免费| 91久久精品视频| 亚洲国产精品婷婷久久| 久久这里只有精品久久| 久久精品国产一区二区三区日韩| 国产一成人精品福利网站| 99精品人妻无码专区在线视频区 | 亚洲国产精品婷婷久久| 青青青国产依人精品视频| 国产精品臀控福利在线观看| 国产精品无码A∨精品影院| 久久99精品久久久久久久不卡| 欧美精品中文字幕亚洲专区| 日韩国产精品无码一区二区三区 | 久久久久国产成人精品亚洲午夜 | 国产午夜福利精品久久| 国产欧美日本精品| 久久99精品国产99久久6| 久久久91人妻无码精品蜜桃HD| 狠狠色伊人久久精品综合网| 黑人巨茎精品欧美一区二区| 欧美黑人巨大videos精品| 婷婷成人国产精品| 一本色道久久88—综合亚洲精品| 亚洲AV无码成人精品区在线观看| 精品永久久福利一区二区| 国产精品毛片一区二区三区| 国产成人精品日本亚洲专一区| 久久国产乱子伦精品免费强| 97精品伊人久久久大香线蕉|