有如下表格
辖区       网吧名称
辖区一     网吧一
辖区一     网吧二
辖区一     网吧三
辖区二     网吧四
辖区三     网吧五
辖区三     网吧六
辖区四     null(没有网吧)
辖区五     null(没有网吧)求一条SQL语句统计各辖区的数量,想得到如下结果
辖区    网吧数量
辖区一 3
辖区二 1
辖区三 2
辖区四 0
辖区五 0

解决方案 »

  1.   

    select 辖区,
    网吧数量=sum(case when len(inull(网吧名称,''))!=0 then 1 else 0)
    from tb
    group by 辖区
      

  2.   

    select 辖区,sum(case when 网吧名称 is null then 0 else l end)from TB
    group by 辖区
      

  3.   

    select 辖区,
    网吧数量=sum(case when 网吧名称 is null then 0 else 1)
    from tb
    group by 辖区
    order by 1
      

  4.   

    if not object_id('tb') is null
    drop table tb
    Go
    Create table tb([辖区] nvarchar(3),[网吧名称] nvarchar(3))
    Insert tb
    select N'辖区一',N'网吧一' union all
    select N'辖区一',N'网吧二' union all
    select N'辖区一',N'网吧三' union all
    select N'辖区二',N'网吧四' union all
    select N'辖区三',N'网吧五' union all
    select N'辖区三',N'网吧六' union all
    select N'辖区四',null union all
    select N'辖区五',null
    Go
    Select [辖区],
           sum(case when [网吧名称]is not null then 1 else 0 end)网吧数量
    from tb group by [辖区]
    /*
    辖区   网吧数量
    ---- -----------
    辖区一  3
    辖区二  1
    辖区三  2
    辖区五  0
    辖区四  0(5 個資料列受到影響)*/
      

  5.   

    如果null有可能是字符串if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([辖区] varchar(6),[网吧名称] varchar(6))
    insert [tb]
    select '辖区一','网吧一' union all
    select '辖区一','网吧二' union all
    select '辖区一','网吧三' union all
    select '辖区二','网吧四' union all
    select '辖区三','网吧五' union all
    select '辖区三','网吧六' union all
    select '辖区四','null' union all
    select '辖区五',null
    goselect 辖区,
    网吧数量=sum(case when len(isnull(网吧名称,''))!=0 and upper(网吧名称)!='NULL' then 1 else 0 end)
    from tb
    group by 辖区/**
    辖区     网吧数量        
    ------ ----------- 
    辖区二    1
    辖区三    2
    辖区四    0
    辖区五    0
    辖区一    3(所影响的行数为 5 行)
    **/
      

  6.   

    ----------------------------------------------------------------
    -- Author  :SQL77(只为思齐老)
    -- Date    :2010-02-23 10:54:47
    -- Version:
    --      Microsoft SQL Server  2000 - 8.00.194 (Intel X86) 
    -- Aug  6 2000 00:57:48 
    -- Copyright (c) 1988-2000 Microsoft Corporation
    -- Desktop Engine on Windows NT 5.1 (Build 2600: Service Pack 3)
    --
    ----------------------------------------------------------------
    --> 测试数据:#tb
    if object_id('tempdb.dbo.#tb') is not null drop table #tb
    go 
    create table #tb([辖区] varchar(6),[网吧名称] varchar(6))
    insert #tb
    select '辖区一','网吧一' union all
    select '辖区一','网吧二' union all
    select '辖区一','网吧三' union all
    select '辖区二','网吧四' union all
    select '辖区三','网吧五' union all
    select '辖区三','网吧六' union all
    select '辖区四',null union all
    select '辖区五',null
    --------------开始查询--------------------------select 
    辖区,
    count(网吧名称)as num 
    from #tb 
    group by  辖区 order by charindex(right(辖区,1),'一二三四五')
    ----------------结果----------------------------
    /* (所影响的行数为 8 行)辖区     num         
    ------ ----------- 
    辖区一    3
    辖区二    1
    辖区三    2
    辖区四    0
    辖区五    0(所影响的行数为 5 行)警告: 聚合或其它 SET 操作消除了空值。
    */
      

  7.   

     select count(网吧名称) ;group by 辖区  
      

  8.   

    select 辖区, county(网吧名称)from tb group by 辖区