zoukankan      html  css  js  c++  java
  • Oracle数据去重

    一、完全重复数据去重方法

       具体思路是,首先创建一个临时表,然后将DISTINCT之后的表数据插入到这个临时表中;然后清空原表数据;再讲临时表中的数据插入到原表中;最后删除临时表。

      对于表中完全重复数据去重,可以采用以下SQL语句。

          --Code

         CREATE TABLE "#temp" AS (SELECTDISTINCT * FROM 表名);   --创建临时表,并把DISTINCT 去重后的数据插入到临时表中

         truncate TABLE 表名;   --清空原表数据

         INSERT INTO 表名 (SELECT * FROM "#temp");   --将临时表数据插入到原表中

        DROP TABLE "#temp";   --删除临时表

     

    二、部分数据去重方法 

    我们可以考虑建立临时表,将需要判断重复的字段、rowid插入临时表中,然后删除的时候在进行比较。

    createtable 临时表 as
     
    select a.字段1,a.字段2,MAX(a.ROWID) dataid from 正式表 a GROUPBY a.字段1,a.字段2;
     
    deletefrom 表名 a
     
    where a.rowid !=
     
    (
     
    select b.dataid from 临时表 b
     
    where a.字段1 = b.字段1 and
     
    a.字段2 = b.字段2
     
    );
     
    commit;

    实例:

    -- 根据MAX(a.rowid)筛选重复的数据,获得一张数据不重复的临时表
    create table 临时表 as select a.ip,a.port,MAX(a.ROWID) dataid from ipresult a GROUP BY a.ip,a.port;
    -- 删除正式表中重复数据,只保留最新的一条数据 delete from ipresult a where a.rowid
    != ( select b.dataid from 临时表 b where a.ip = b.ip and a.port= b.port );
    --删除临时表并提交 drop table 临时表; commit;

    引用资料:

          Oracle数据库中重复数据删除方法:部分去重+完全去重’

  • 相关阅读:
    NOIP2016 愤怒的小鸟
    LCIS code force 10D
    UVA 1398
    uva1382 Distant Galaxy
    洛谷-3930(我在洛谷上也写了题解)
    HDU-1505 City Game
    导弹拦截n logn的算法(单调性)洛谷1020
    POJ 1182 食物链
    POJ
    1202. 交换字符串中的元素
  • 原文地址:https://www.cnblogs.com/Dream2hc/p/knowledge575580.html
Copyright © 2011-2022 走看看