类型和等级  a    b    c d
      一级
      二级
      三级
      小计统计a有多少个一级的多少个2级的等
b统计有多少个一级的多少个2级的。。type 字段:1.a,2.b,3.c,4.d;   --1代表a,2代表b,等....class 字段:1.一级, 2. 二级,3.三级, 4.四级;不要用 select @a=count(*)  from table where class='一级' and type= 1

解决方案 »

  1.   

    select count(1) from tab group class
      

  2.   

    TYPE  CHAR(1)     类型 必填字段LENGTH=1   1.a,2.b,3.c,4.d
    CLASS CHAR(1) 等级类型 必填字段LENGTH=1   1.一级, 2. 二级,3.三级, 4.四级
      

  3.   

    declare @t table(type varchar(10),class varchar(10))
    insert into @t select 'a','一级'
    insert into @t select 'b','一级'
    insert into @t select 'c','一级'
    insert into @t select 'a','一级'
    insert into @t select 'c','二级'
    insert into @t select 'b','三级'select class,
           a=sum(case type when 'a' then 1 else 0 end),
           b=sum(case type when 'b' then 1 else 0 end),
           c=sum(case type when 'c' then 1 else 0 end),
           d=sum(case type when 'd' then 1 else 0 end)
    from @t
    group by class?
      

  4.   

    insert into @t select 'a','一级'
    insert into @t select 'c','二级'
    insert into @t select 'b','三级'
    这里是什么意思啊???
    还有在else 0 end  这里是什么意思啊?
    需要建立一个参数表???
      

  5.   

    insert into @t select 'a','一级'
    insert into @t select 'c','二级'
    insert into @t select 'b','三级'
    --这个是插入测试数据的,不用管它
    sum 可以换成count() 吗改了的话,就不能case when ...这样写了
      

  6.   

    那但是我的表里面的数据是用1,2,3,4来代表意思的啊
    这样吧可能清楚点:
    类型和等级  外语    语文 数学    体育
          一级
          二级
          三级
          小计表结构:
    字段名称                                      说明
      TYPE   CHAR(1)  科目类型  必填字段LENGTH=1  1:外语;2:语文;3:数学;4:体育
      CLASS  CHAR(1)  等级类型  必填字段LENGTH=1  1:一级;2:二级;3:三级;4:四级表里保存的数据为 1,2,3,4等  不是显明的字段类型;
    要是这样用sum 的话 那数据就不对了啊?
      

  7.   

    统计的单位是人数
    我觉得好像只能用count() 1的个数 和2等个数方法
    但是那样我要也多少个存储过程啊
      

  8.   

    select class,
           count(case type when 'a' then type end) a,
           count(case type when 'b' then type end) b,
           count(case type when 'c' then type end) c,
           count(case type when 'd' then type end) d
    from table 
    group by class