有表T1,结构如下:
A   B
a1  0
a1  NULL
a2  NULL
a2  NULLselect A,count(B) from T1
where B=0
group by A
本想得到如下结果:
a1 1
a2 0
但不如意,只得到a1 1的结果,不知道怎么样才能得到想要的结果?

解决方案 »

  1.   

    Select A, Count(B) From T1
    Where IsNull(B, 0)=0
    Group by A
      

  2.   

    这么写就ok了,但是不知道你的意思是什么,我只是猜测的,是不是null不计算条数啊,下面的语句就是这个意思:
    select A,count(B) from T1 group by A
      

  3.   

    感谢 cncharles(旺仔) ,不过对于IsNull(B, 0)=0这一句不是很理解,当B为Null的时候,这句就是0=0,显然是成立的,但是既然条件为真,统计出来怎么还会是:
    a1 1
    a2 0
    呢?
      

  4.   

    Select A, Count(IsNull(B, 0)) From T1
    Where IsNull(B, 0)=0
    Group by A//如果B是null就把它转换为0
      

  5.   

    我的意思其实是希望先Group by,再计算where
      

  6.   

    Select T1.A, IsNull(T2.B2, 0) As B
    From T1
    Left Join (
    Select A, Count(B) As B2
    From T1
    Where B=0
    Group by A ) As T2
    On T1.A=T2.A
    Order By T1.A
      

  7.   

    不好意思, 上面都没搞错了.下面的应该是你想要的.Select T2.A, IsNull(T3.A1, 0)
    From (Select A From T1 Group by A) As T2
    Left Join (Select A, Count(A) As A1
          From T1 Where B=0 Group By A) As T3
    On T2.A=T3.A
    Order by T2.A
      

  8.   

    晕倒,也不用怎么复杂吧,真的是先Group by,然后再where,晕晕滴....