例如有一表: a
          名称   数量     
          
          sss    33
          sss    75
          sss    55
          aaa    65
          aaa    78
          ddd    55要按名称分组查出数量大于60的个数,空值要显示为零 我用 select 名称,count(*) from a where 数量 > 60 group by 名称  这样出来的结果是
          sss    1
          aaa    2这样就把空值给漏掉了 我想要如下结果:
          sss    1
          aaa    2
          ddd    0
该怎么查询

解决方案 »

  1.   

    --> 测试数据:#tb
    if object_id('tempdb.dbo.#tb') is not null drop table #tb
    create table #tb([名称] varchar(3),[数量] int)
    insert #tb
    select 'sss',33 union all
    select 'sss',75 union all
    select 'sss',55 union all
    select 'aaa',65 union all
    select 'aaa',78 union all
    select 'ddd',55select 名称,sum(case when 数量>60 then 1 else 0 end) from #tb  
    group by 名称
      

  2.   

    select distinct a.名称,isnull(b.Num,0) from a
    left join 
    (select 名称,count(*) as Num from a where 数量 > 60 group by 名称) b
    on a.名称=b.名称
      

  3.   

    group by all 名称这就行了
      

  4.   

    select 名称,count(名称)
    from tb
    where 数量>60
    group by all 名称