把tab分开,先统计A有重复记录的,再统计A没有重复的,相加select sum(sum1) from 
(
  select count(*) as sum1
  from tab1 where B=1 and A in
  (
    select A
    from tab1
    group by A
    having count(A)>1
  )
union all
   select count(*) as sum1   
   from tab1 where A in
   (
      select A
      from tab1
      group by A
      having count(A)=1
   )
);

解决方案 »

  1.   

    select a,b,count(*) from(
    select * from scott.tt t
    minus
    select * from scott.tt t where
    a in
    (select a cnt from scott.tt t
    group by a
    having count(*) >1
    )
    and b <> 1
    )group by a,b
      

  2.   

    a,b是主键,本身具有唯一性,不需要group by,有待改进.
      

  3.   

    TO: maohaisheng() 
    写的实在妙!
      

  4.   

    因为A,B为主键,那么A重复的记录中最多只可能有一条记录的B为1,同时从楼主的题意看,A重复的记录中至少有一条记录的B为1,所以只要简单的使用下面的SQL就OK了。select count(distinct A)  from T1;