Name    PartyBranch    TypeNo    a       第一支部      正式党员      
 b       第一支部      积极分子      
 c       第一支部      正式党员      
 d       第一支部      正式党员      
 e       第一支部      正式党员      
 f       第二支部      正式党员      
 g       第二支部      正式党员      
 h       第二支部      积极分子      
 i       第二支部      正式党员      PartyBranch是党支部的意思
TypeNo是党员类型
GloryNo是否优秀
现在需要统计 各支部人数 和 各支部正式党员数
要怎么写sql语句,请各位大虾帮忙~~~~

解决方案 »

  1.   

    select 
        PartyBranch,
        count(*) as 支部人数,
        sum(case TypeNo when '正式党员' then 1 else 0 end) as 正式党员数
    from 
        tname 
    group by 
        PartyBranch
      

  2.   

    典型的交叉报表列子
    declare @tempsql varchar(8000)
    set @tempsql = ''
    select @tempsql = @tempsql + '[' + zymc + '] = (select count(*) from table1 b where b.name = a.name and b.zymc = ''' + zymc + '''),' from table1 group by zymc
    set @tempsql = left(@tempsql,len(@tempsql)-1) 
    print @tempsql
    -------------------------
    set @tempsql = 'select a.name ,' + @tempsql 
            + ' from table1 a'
            + ' group by a.name'print @tempsqlexec(@tempsql)修改下应该就可以解决你的问题
      

  3.   


    --> 测试数据:@table
    declare @table table([Name] varchar(1),[PartyBranch] varchar(8),[TypeNo] varchar(8))
    insert @table
    select 'a','第一支部','正式党员' union all
    select 'b','第一支部','积极分子' union all
    select 'c','第一支部','正式党员' union all
    select 'd','第一支部','正式党员' union all
    select 'e','第一支部','正式党员' union all
    select 'f','第二支部','正式党员' union all
    select 'g','第二支部','正式党员' union all
    select 'h','第二支部','积极分子' union all
    select 'i','第二支部','正式党员'select PartyBranch,count(1) as 人数,
    sum(case TypeNo when '正式党员' then 1 else 0 end) as  正式党员数
    from @table
    group by PartyBranch
    --结果
    ----------------------------------
    第二支部 4 3
    第一支部 5 4
      

  4.   

    谢谢你,你的sql比较简单,容易理解。不过还有些支部没党员,要怎么统计,人数是NULL怎么全都显示出来,我添上all也显示不出来,请帮忙~~
      

  5.   

    Create table caidaniel(Name nvarchar(4),
    PartyBranch nvarchar(10),
    TypeNo nvarchar(10))select * from caidanielinsert caidaniel select 'a','第一支部','正式党员'
    union all
    select 'b','第一支部','积极分子'
    union all
    select 'c','第一支部','正式党员'
    union all
    select 'd','第一支部','正式党员'
    union all
    select 'e','第一支部','正式党员'
    union all
    select 'f','第二支部','正式党员'
    union all
    select 'g','第二支部','正式党员'
    union all
    select 'h','第二支部','积极分子'
    union all
    select 'i','第二支部','正式党员'Select Partybranch,count(*) from caidaniel group by Partybranch
    Select Partybranch,count(*) from caidaniel 
    where typeno='正式党员'
    group by Partybranch
      

  6.   


    declare @table table([Name] varchar(1),[PartyBranch] varchar(8),[TypeNo] varchar(8))
    insert @table
    select 'a','第一支部','正式党员' union all
    select 'b','第一支部','积极分子' union all
    select 'c','第一支部','正式党员' union all
    select 'd','第一支部','正式党员' union all
    select 'e','第一支部',null union all
    select 'f','第二支部', null union all
    select 'g','第二支部','正式党员' union all
    select 'h','第二支部','积极分子' union all
    select 'i','第二支部','正式党员';select PartyBranch 支部, COUNT(1) 人数, SUM(case when TypeNo = '正式党员' then 1 else 0 end)
    正式党员数 from @table group by PartyBranch order by PartyBranch desc--结果
    支部       人数          正式党员数
    -------- ----------- -----------
    第一支部     5           3
    第二支部     4           2
      

  7.   


    if exists (select 1 from sysobjects where name = 'tb' and xtype = 'u')
    drop table tb
    create table tb([Name] varchar(1),[PartyBranch] varchar(8),[TypeNo] varchar(8))
    insert into tb
    select 'a','第一支部','正式党员' union all
    select 'b','第一支部','积极分子' union all
    select 'c','第一支部','正式党员' union all
    select 'd','第一支部','正式党员' union all
    select 'e','第一支部','正式党员' union all
    select 'f','第二支部', '正式党员' union all
    select 'g','第二支部','正式党员' union all
    select 'h','第二支部','积极分子' union all
    select 'i','第二支部','正式党员';declare @sql nvarchar(2000)
    select @sql = ISNULL(@sql +', ','select PartyBranch 支部, COUNT(1) 人数, ')
    +'SUM(case when TypeNo = '''+TypeNo+''' then 1 else 0 end) '+TypeNo+' '
    from (select distinct TypeNo from tb) texec (@sql + ' from tb group by PartyBranch')--结果
    (9 row(s) affected)
    支部       人数          积极分子        正式党员
    -------- ----------- ----------- -----------
    第二支部     4           1           3
    第一支部     5           1           4(2 row(s) affected)
      

  8.   

    if OBJECT_ID('tb') is not null drop table tb
    GO
    Create table tb([Name] varchar(1),[PartyBranch] varchar(8),[TypeNo] varchar(8))
    GO
    insert tb
    select 'a','第一支部','正式党员' union all
    select 'b','第一支部','积极分子' union all
    select 'c','第一支部','正式党员' union all
    select 'd','第一支部','正式党员' union all
    select 'e','第一支部','正式党员' union all
    select 'f','第二支部','正式党员' union all
    select 'g','第二支部','正式党员' union all
    select 'h','第二支部','积极分子' union all
    select 'i','第二支部','正式党员'
    GO
    select 
    PartyBranch,
     count(TypeNo) as 人数,
     sum(case TypeNo when '正式党员' then 1 else 0 end )as 正式党员
    from tb
    group by PartyBranch
    order by PartyBranch desc