亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

oracle數據庫去除重復數據常用的方法總結

瀏覽:117日期:2023-03-12 15:25:05
目錄
  • 創建測試數據
  • 針對指定列,查出去重后的結果集
    • distinct
    • row_number()
  • 針對指定列,查出所有重復的行
    • count having
    • count over
  • 刪除所有重復的行
    • 刪除重復數據并保留一條
      • 分析函數法
      • group by
    • 總結

      創建測試數據

      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_3123123523102030

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

      distinct

      select distinct t1.* from nayi224_180824 t1;
      
      COL_1COL_2COL_3102030123523

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

      select distinct t1.col_2, col_3 from nayi224_180824 t1
      
      COL_2COL_3232030

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

      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_3RN12311020301

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

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

      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_3123123523

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

      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_3RN123312335233

      只需要查一次表,推薦。

      刪除所有重復的行

      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去除重復數據內容請搜索以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持!

      標簽: Oracle
      主站蜘蛛池模板: 在线播放 亚洲 | 亚洲爱爱视频 | 欧美黄色免费网站 | 欧美特级| 亚州欧美 | 国产亚洲青色国产 | 亚洲高清国产品国语在线观看 | 久久夜色tv网站免费影院 | 毛片让我看一下毛片 | 国产一级毛片视频在线! | 黄色a级毛片 | 六月久久 | 欧美性性性性性色大片免费的 | 国产在线麻豆一区二区 | 国产成人精品视频免费大全 | 999久久66久6只有精品 | 日韩一级片在线免费观看 | 日韩在线观看一区二区不卡视频 | 特级毛片全部免费播放a一级 | www.一级毛片 | www.亚洲综合 | 国产精品亚洲色图 | 国产精品成人免费 | 欧美日韩一区二区三区在线 | 免费无毒片在线观看 | 国产在线a不卡免费视频 | 最新亚洲手机在线人成网站 | 色综合夜夜嗨亚洲一二区 | 国产精品一页 | 国产精品麻豆一区二区 | 精品国产亚一区二区三区 | 在线 中文字幕 日韩 欧美 | 国产一区二区三区欧美精品 | 高清色图 | 一级a级国产不卡毛片 | 五月婷婷网| 亚洲人成一区二区不卡 | 1300部小u女视频免费 | 男人午夜免费视频 | 手机看片自拍自自拍日韩免费 | 国产婷婷色 |