现有表table
有字段a,d
用select count(distinct a) from table
  select count(distinct d) from table
得到结果count(distinct a) 记录数大于 count(distinct d)
因为a 和 d 应该是一一对应关系,存在上诉结果是因为历史遗留问题。
请问各位前辈如何用SQL语句查出 1、d 对应的多个 a  2、多个 b 对应的 a.新手初次发贴,不知描述清楚没有...见谅。

解决方案 »

  1.   

     2、多个 b 对应的 a修改为2、多个a 对应的 d
      

  2.   

    --既然说a 和 d 应该是一一对应关系,怎么又查出 1、d 对应的多个 a  2、多个 b 对应的 a. select  distinct * from tb a where exists(select 1 from tb where a=a.a and d<>a.d)select  distinct * from tb a where exists(select 1 from tb where d=a.d and a<>a.a)
      

  3.   

    表达不太清楚,不好意思
    这样说吧,本身应该是一一对应,因为没有更新后,没有清楚历史记录造成的。
    a 和 d 是对应关系,但a、d、的值不同,我想大概意思就是差 table中 重复 的 a 记录
      

  4.   

    表达不太清楚,不好意思
    这样说吧,本身应该是一一对应,因为没有更新后,没有清楚历史记录造成的。
    a 和 d 是对应关系,但a、d、的值不同,我想大概意思就是查 table中 相同值 的 a 记录
      

  5.   

    1.
    select a,d from table t1 where exists(select 1 from table where a=t1.a group by a having count(*)>1)
    2.
    上面把a,d互换.
      

  6.   

    create table tb(a int,d int)
    insert into tb select 1,3 union all select 1,4 union all select 2,6 union all select 4,7 union all select 4,9 union all select 6,12
    go
    select a,d from tb t1 where exists(select 1 from tb where a=t1.a group by a having count(*)>1)
    go
    drop table tb
    /*
    a           d
    ----------- -----------
    1           3
    1           4
    4           7
    4           9(4 行受影响)
    */
      

  7.   

    d 对应的多个 a  select count(*),b,a
    from table 
    group by b,a多个 b 对应的 a. select count(cnt)
    from
    (
    select *,row_number()over(partition by b,a order by b) cnt
    from table
    ) A
    group by b,a