select
isnull(a.A,b.A) as A,
(a.N1 + b.N2) as NumN
from
(select A,count(N) as N1 from t where N=1 group by A) a
full outer join
(select A,count(distinct N) as N2 from t where N<>1 group by A) b
on
a.A = b.A
isnull(a.A,b.A) as A,
(a.N1 + b.N2) as NumN
from
(select A,count(N) as N1 from t where N=1 group by A) a
full outer join
(select A,count(distinct N) as N2 from t where N<>1 group by A) b
on
a.A = b.A
from (
select * from t where n=1
union all
select A,N from t where n<>1 group by A,N) a
group by a.A