create table INPUT_USER_INFO
(
  ID            NUMBER not null,
  FIRSTNAME     VARCHAR2(30),
  CERTNO        VARCHAR2(30),
  NATIONALITY   VARCHAR2(50),
  GENDER        VARCHAR2(4),
  STATION       VARCHAR2(100),
  CR_STANDARD   VARCHAR2(20),
  FLTNO         VARCHAR2(20),
  GOTO_TYPE     VARCHAR2(20),
  LASTNAME      VARCHAR2(30),
  START_TIME    VARCHAR2(20),
  ARRIVE_TIME   VARCHAR2(20),
  GROUPNAME     VARCHAR2(100),
  CUS_TYPE      VARCHAR2(100),
  TRAFFICLEVEL  VARCHAR2(20),
  RES_UNIT      VARCHAR2(100),
  RESTADDRESS   VARCHAR2(100),
  NORMALBAG     VARCHAR2(4),
  ILLEGIMATEBAG VARCHAR2(100),
  START_CITY    VARCHAR2(50),
  ARRIVE_CITY   VARCHAR2(50)
)
上面是表字段,要求表中按certno(证件号)筛选数据,一个证件号可以存在多条数据,但是每条数据对应字段不能完全一样,也不能存在一条数据包含另外一条数据的情况(即非空字段的值相等,其中一条非空字段比另外一条多)。目前表中存在很多不符合要求的数据,怎么写sql来删除这样的不符合要求的数据呢?等待高手

解决方案 »

  1.   

    delete from INPUT_USER_INFO a
     where not exists
     (select 1
              from INPUT_USER_INFO b
             where a.certno = b.certno
             group by certno
            having max(length(FIRSTNAME || CERTNO || NATIONALITY || GENDER || STATION || CR_STANDARD || FLTNO || GOTO_TYPE || LASTNAME || START_TIME || ARRIVE_TIME || GROUPNAME || CUS_TYPE || TRAFFICLEVEL || RES_UNIT || RESTADDRESS || NORMALBAG || ILLEGIMATEBAG || START_CITY || ARRIVE_CITY)) = length(a.FIRSTNAME || a.CERTNO || a.NATIONALITY || a.GENDER || a.STATION || a.CR_STANDARD || a.FLTNO || a.GOTO_TYPE || a.LASTNAME || a.START_TIME || a.ARRIVE_TIME || a.GROUPNAME || a.CUS_TYPE || a.TRAFFICLEVEL || a.RES_UNIT || a.RESTADDRESS || a.NORMALBAG || a.ILLEGIMATEBAG || a.START_CITY || a.ARRIVE_CITY))
      

  2.   


    你参考此贴:--按某一字段分组取最大(小)值所在行的数据
    --(爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开) 2007-10-23于浙江杭州)
    /*
    数据如下:
    name val memo
    a    2   a2(a的第二个值)
    a    1   a1--a的第一个值
    a    3   a3:a的第三个值
    b    1   b1--b的第一个值
    b    3   b3:b的第三个值
    b    2   b2b2b2b2
    b    4   b4b4
    b    5   b5b5b5b5b5
    */
    --创建表并插入数据:
    create table tb(name varchar(10),val int,memo varchar(20))
    insert into tb values('a',    2,   'a2(a的第二个值)')
    insert into tb values('a',    1,   'a1--a的第一个值')
    insert into tb values('a',    3,   'a3:a的第三个值')
    insert into tb values('b',    1,   'b1--b的第一个值')
    insert into tb values('b',    3,   'b3:b的第三个值')
    insert into tb values('b',    2,   'b2b2b2b2')
    insert into tb values('b',    4,   'b4b4')
    insert into tb values('b',    5,   'b5b5b5b5b5')
    go--一、按name分组取val最大的值所在行的数据。
    --方法1:
    select a.* from tb a where val = (select max(val) from tb where name = a.name) order by a.name
    --方法2:
    select a.* from tb a where not exists(select 1 from tb where name = a.name and val > a.val)
    --方法3:
    select a.* from tb a,(select name,max(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
    --方法4:
    select a.* from tb a inner join (select name , max(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
    --方法5
    select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name
    /*
    name       val         memo                 
    ---------- ----------- -------------------- 
    a          3           a3:a的第三个值
    b          5           b5b5b5b5b5
    */--二、按name分组取val最小的值所在行的数据。
    --方法1:
    select a.* from tb a where val = (select min(val) from tb where name = a.name) order by a.name
    --方法2:
    select a.* from tb a where not exists(select 1 from tb where name = a.name and val < a.val)
    --方法3:
    select a.* from tb a,(select name,min(val) val from tb group by name) b where a.name = b.name and a.val = b.val order by a.name
    --方法4:
    select a.* from tb a inner join (select name , min(val) val from tb group by name) b on a.name = b.name and a.val = b.val order by a.name
    --方法5
    select a.* from tb a where 1 > (select count(*) from tb where name = a.name and val < a.val) order by a.name
    /*
    name       val         memo                 
    ---------- ----------- -------------------- 
    a          1           a1--a的第一个值
    b          1           b1--b的第一个值
    */--三、按name分组取第一次出现的行所在的数据。
    select a.* from tb a where val = (select top 1 val from tb where name = a.name) order by a.name
    /*
    name       val         memo                 
    ---------- ----------- -------------------- 
    a          2           a2(a的第二个值)
    b          1           b1--b的第一个值
    */--四、按name分组随机取一条数据。
    select a.* from tb a where val = (select top 1 val from tb where name = a.name order by newid()) order by a.name
    /*
    name       val         memo                 
    ---------- ----------- -------------------- 
    a          1           a1--a的第一个值
    b          5           b5b5b5b5b5
    */--五、按name分组取最小的两个(N个)val
    select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val < a.val ) order by a.name,a.val
    select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val) order by a.name,a.val
    select a.* from tb a where exists (select count(*) from tb where name = a.name and val < a.val having Count(*) < 2) order by a.name,a.val
    /*
    name       val         memo                 
    ---------- ----------- -------------------- 
    a          1           a1--a的第一个值
    a          2           a2(a的第二个值)
    b          1           b1--b的第一个值
    b          2           b2b2b2b2
    */--六、按name分组取最大的两个(N个)val
    select a.* from tb a where 2 > (select count(*) from tb where name = a.name and val > a.val ) order by a.name,a.val
    select a.* from tb a where val in (select top 2 val from tb where name=a.name order by val desc) order by a.name,a.val
    select a.* from tb a where exists (select count(*) from tb where name = a.name and val > a.val having Count(*) < 2) order by a.name , a.val
    /*
    name       val         memo                 
    ---------- ----------- -------------------- 
    a          2           a2(a的第二个值)
    a          3           a3:a的第三个值
    b          4           b4b4
    b          5           b5b5b5b5b5
    */
    --七,如果整行数据有重复,所有的列都相同。
    /*
    数据如下:
    name val memo
    a    2   a2(a的第二个值)
    a    1   a1--a的第一个值
    a    1   a1--a的第一个值
    a    3   a3:a的第三个值
    a    3   a3:a的第三个值
    b    1   b1--b的第一个值
    b    3   b3:b的第三个值
    b    2   b2b2b2b2
    b    4   b4b4
    b    5   b5b5b5b5b5
    */
    --在sql server 2000中只能用一个临时表来解决,生成一个自增列,先对val取最大或最小,然后再通过自增列来取数据。
    --创建表并插入数据:
    create table tb(name varchar(10),val int,memo varchar(20))
    insert into tb values('a',    2,   'a2(a的第二个值)')
    insert into tb values('a',    1,   'a1--a的第一个值')
    insert into tb values('a',    1,   'a1--a的第一个值')
    insert into tb values('a',    3,   'a3:a的第三个值')
    insert into tb values('a',    3,   'a3:a的第三个值')
    insert into tb values('b',    1,   'b1--b的第一个值')
    insert into tb values('b',    3,   'b3:b的第三个值')
    insert into tb values('b',    2,   'b2b2b2b2')
    insert into tb values('b',    4,   'b4b4')
    insert into tb values('b',    5,   'b5b5b5b5b5')
    goselect * , px = identity(int,1,1) into tmp from tbselect m.name,m.val,m.memo from
    (
      select t.* from tmp t where val = (select min(val) from tmp where name = t.name)
    ) m where px = (select min(px) from
    (
      select t.* from tmp t where val = (select min(val) from tmp where name = t.name)
    ) n where n.name = m.name)drop table tb,tmp/*
    name       val         memo
    ---------- ----------- --------------------
    a          1           a1--a的第一个值
    b          1           b1--b的第一个值(2 行受影响)
    */
    --在sql server 2005中可以使用row_number函数,不需要使用临时表。
    --创建表并插入数据:
    create table tb(name varchar(10),val int,memo varchar(20))
    insert into tb values('a',    2,   'a2(a的第二个值)')
    insert into tb values('a',    1,   'a1--a的第一个值')
    insert into tb values('a',    1,   'a1--a的第一个值')
    insert into tb values('a',    3,   'a3:a的第三个值')
    insert into tb values('a',    3,   'a3:a的第三个值')
    insert into tb values('b',    1,   'b1--b的第一个值')
    insert into tb values('b',    3,   'b3:b的第三个值')
    insert into tb values('b',    2,   'b2b2b2b2')
    insert into tb values('b',    4,   'b4b4')
    insert into tb values('b',    5,   'b5b5b5b5b5')
    goselect m.name,m.val,m.memo from
    (
      select * , px = row_number() over(order by name , val) from tb
    ) m where px = (select min(px) from
    (
      select * , px = row_number() over(order by name , val) from tb
    ) n where n.name = m.name)drop table tb/*
    name       val         memo
    ---------- ----------- --------------------
    a          1           a1--a的第一个值
    b          1           b1--b的第一个值(2 行受影响)
    */如果看明白了,假设按照FIRSTNAME分组,保留最小的ID
    delete INPUT_USER_INFO from INPUT_USER_INFO t
    where id not in(select min(id) from INPUT_USER_INFO where FIRSTNAME = t.FIRSTNAME)假设按照FIRSTNAME分组,保留最大的ID
    delete INPUT_USER_INFO from INPUT_USER_INFO t
    where id not in(select max(id) from INPUT_USER_INFO where FIRSTNAME = t.FIRSTNAME)
      

  3.   


    select * from (
               select a.*,row_number()over(partition by FIRSTNAME ,CERTNO ,NATIONALITY ,GENDER ,--所有栏位
    order by id) rn 
               from INPUT_USER_INFO a
              ) 
       where rn!=1
      

  4.   

    你可以先看看查出的数据是否符合要求
    select * from INPUT_USER_INFO a
     where not exists
     (select 1
              from INPUT_USER_INFO b
             where a.certno = b.certno
             group by certno
            having max(length(FIRSTNAME || CERTNO || NATIONALITY || GENDER || STATION || CR_STANDARD || FLTNO || GOTO_TYPE || LASTNAME || START_TIME || ARRIVE_TIME || GROUPNAME || CUS_TYPE || TRAFFICLEVEL || RES_UNIT || RESTADDRESS || NORMALBAG || ILLEGIMATEBAG || START_CITY || ARRIVE_CITY)) = length(a.FIRSTNAME || a.CERTNO || a.NATIONALITY || a.GENDER || a.STATION || a.CR_STANDARD || a.FLTNO || a.GOTO_TYPE || a.LASTNAME || a.START_TIME || a.ARRIVE_TIME || a.GROUPNAME || a.CUS_TYPE || a.TRAFFICLEVEL || a.RES_UNIT || a.RESTADDRESS || a.NORMALBAG || a.ILLEGIMATEBAG || a.START_CITY || a.ARRIVE_CITY))
      

  5.   


    这条sql只是能够删除两条完全相同的数据,那种子集被包含关系的数据筛选不出来啊!
      

  6.   


    使用一条SQL语句基本没有什么好办法,楼主有没有考虑过使用代码来实现该功能呢?