如果有两条记录的USENAME相同,那要比较specialty1这个字段,如果是>0的就保留第一条记录,其余的都删除。

解决方案 »

  1.   

    如果不需要判断相同记录的完整性,只保留一条username相同的字段
    并且有个唯一的ID字段delete L  from 表 L ,表 R where L.username=R.username and L.ID > R.ID
      

  2.   

    这个表没有ID主键而且也只有一个表,没有表R。只有表L。
      

  3.   

    zjcxc(: 邹建 :) 要判断其他字段我后面可以再自己加是。
      

  4.   

    没有id你可以加一个,便于处理表 指的是你的表名
    L 是“表”的别名
      

  5.   

    --表中要有主键才行,因为可能有重复的,满足最优条件的记录--假设id为主键--删除处理
    delete 表 from 表 a left join(
    select id=min(id) from 表 a join(
    select 字段1,aa=max(
    case 字段2 when 0 then 0 else 1 end
    +case 字段3 when 0 then 0 else 1 end
    +case 字段4 when 0 then 0 else 1 end)
    from 表 group by 字段1
    )b on a.字段1=b.字段1 and b.aa=(
    case 字段2 when 0 then 0 else 1 end
    +case 字段3 when 0 then 0 else 1 end
    +case 字段4 when 0 then 0 else 1 end)
    group by b.字段1
    )b on a.id=b.id
    where b.id is null
      

  6.   

    --测试--测试数据
    create table 表(id int identity(1,1),字段1 int,字段2 int,字段3 int,字段4 int)
    insert 表 select 1,0,0,0
    union all select 1,0,0,0
    union all select 2,0,0,0  
    union all select 2,4,3,1
    union all select 2,0,3,0
    union all select 3,4,2,8
    go--删除处理
    delete 表 from 表 a left join(
    select id=min(id) from 表 a join(
    select 字段1,aa=max(
    case 字段2 when 0 then 0 else 1 end
    +case 字段3 when 0 then 0 else 1 end
    +case 字段4 when 0 then 0 else 1 end)
    from 表 group by 字段1
    )b on a.字段1=b.字段1 and b.aa=(
    case 字段2 when 0 then 0 else 1 end
    +case 字段3 when 0 then 0 else 1 end
    +case 字段4 when 0 then 0 else 1 end)
    group by b.字段1
    )b on a.id=b.id
    where b.id is null--显示处理结果
    select 字段1,字段2,字段3,字段4 from 表
    go--删除测试
    drop table 表/*--测试结果
    字段1         字段2         字段3         字段4         
    ----------- ----------- ----------- ----------- 
    1           0           0           0
    2           4           3           1
    3           4           2           8(所影响的行数为 3 行)
    --*/