表:要求查询出的结果
123456 1 2    
123456 2 2 
123456 1 2 
123456 2 2 
654321 1 2 
654321 2 2注释: 在这个表中,查询出 门号=2 的 卡号(单个卡号在 (类型=1的个数)=(类型=2的个数)的情况下才能把这个卡号也查出来,单个卡号在 (类型=1的个数)<>(类型=2的个数)的时候不能在查询结果中显示.也就是计算单个卡号在2号门那里有没有平衡,只有(类型=1的个数)=(类型=2的个数)的时候才算平衡的)
高手指导一下吧~~~
用SQL命令实现或用DELPHI代码实现都可以!

解决方案 »

  1.   

    http://topic.csdn.net/u/20090608/11/e4ce47a2-a81a-4283-9913-29dd65fb1a1e.html
      

  2.   

    卡号 类型 门号
    fc  ft   fdselect *
    from t where fc in (
      select fc from (
        select a.*,b.ft2 from (
          select fc,count(*) ft1
          from t
          where ft=1
          group by fc
        ) a
        inner join (
          select fc,count(*) ft2
          from t
          where ft=2
          group by fc
        ) b
        on a.fc=b.fc
      ) a where ft1=ft2
    ) and fn=2
      

  3.   

    最后一行应该是:
    ) and fd=2
      

  4.   

    select *
    from t where fc in (
      select fc from (
        select a.*,b.ft2 from (
          select fc,count(*) ft1 --类型为1,门号为2的数量
          from t
          where ft=1 and fd=2
          group by fc
        ) a
        inner join (
          select fc,count(*) ft2 --类型为2,门号为2的数量
          from t
          where ft=2 and fd=2
          group by fc
        ) b
        on a.fc=b.fc
      ) a where ft1=ft2 --数量要相同
    ) and fd=2