我的表里面有 id, xjbh, xh 等字段,我现在发现了大量的 xjbh和xh重复的,例如:
   id    xjbh     xh
    1    10001     1
    2    10001     2
    3    10001     1
    4    10001     3
    5    10002     1
    6    10002     2
    7    10002     1
    8    10003     1
    9    10003     2
    10   10003     1如何找到  id    xjbh   xh
           1    10001   1
           3    10001   1
           5    10002   1
           7    10002   1
           8    10003   1
           10   10003   1其实我的表里xjbh 是可以重复的,但是相同的xjbh的xh是不能重复,所以要找出相同xjbh的重复xh,如何查找,请指教

解决方案 »

  1.   

    SELECT *
    FROM TB T
    WHERE (SELECT COUNT(1) FROM TB WHERE XJBH=T.XJBH AND XH=T.XH)>1
      

  2.   

    --> 测试数据:@tb
    declare @tb table([id] int,[xjbh] int,[xh] int)
    insert @tb
    select 1,10001,1 union all
    select 2,10001,2 union all
    select 3,10001,1 union all
    select 4,10001,3 union all
    select 5,10002,1 union all
    select 6,10002,2 union all
    select 7,10002,1 union all
    select 8,10003,1 union all
    select 9,10003,2 union all
    select 10,10003,1select * from @tb a where  (select count(*) from @tb where  xjbh=a.xjbh and xh=a.xh)>1
    /*
    id          xjbh        xh
    ----------- ----------- -----------
    1           10001       1
    3           10001       1
    5           10002       1
    7           10002       1
    8           10003       1
    10          10003       1(6 行受影响)*/
      

  3.   

    ---测试数据---
    if object_id('[TB]') is not null drop table [TB]
    go
    create table [TB]([id] int,[xjbh] int,[xh] int)
    insert [TB]
    select 1,10001,1 union all
    select 2,10001,2 union all
    select 3,10001,1 union all
    select 4,10001,3 union all
    select 5,10002,1 union all
    select 6,10002,2 union all
    select 7,10002,1 union all
    select 8,10003,1 union all
    select 9,10003,2 union all
    select 10,10003,1
     
    ---查询---
    SELECT *
    FROM TB T
    WHERE (SELECT COUNT(1) FROM TB WHERE XJBH=T.XJBH AND XH=T.XH)>1---结果---
    id          xjbh        xh          
    ----------- ----------- ----------- 
    1           10001       1
    3           10001       1
    5           10002       1
    7           10002       1
    8           10003       1
    10          10003       1(所影响的行数为 6 行)
      

  4.   

    select *
    from ta a
    where exists(select 1 from ta where xjbh = a.xjbh and xh = 1 and id != a.id)
          and xh = 1