学生表(年级,班别,姓名)
我想得到这样的视图(一班数,二班数,三班数,四班数,五班数,六班数)
一班数为:一年级的班数。
我用count(distinct 班别),然后不知道怎么加年级的条件限制!

解决方案 »

  1.   

    create table tb (年级 varchar(20) ,班别 varchar(20),姓名 varchar(20))
    insert into tb
    select '1','1','zhang'
    union all
    select '1','2','wang'
    union all
    select '1','3','li'
    union all
    select '1','3','li'
    union all
    select '2','1','liang'
    union all
    select '2','2','he'
    union all
    select '2','3','chen'
    静态
    select a.年级,
           [一班数]=sum(case [班别] when '1' then 1 else 0 end),
           [二班数]=sum(case [班别] when '2' then 1 else 0 end),
           [三班数]=sum(case [班别] when '3' then 1 else 0 end),
           [四班数]=sum(case [班别] when '4' then 1 else 0 end),
           [五班数]=sum(case [班别] when '5' then 1 else 0 end),
           [六班数]=sum(case [班别] when '6' then 1 else 0 end)
    from tb a
    group by 年级
    动态
    declare @sql varchar(8000)
    set @sql='select 年级'
    select @sql=@sql+',['+班别+']=sum(case 班别 when '''+班别+''' then 1 else 0 end)' from tb group by 班别
    exec(@sql+' from tb group by 年级')
      

  2.   


    create view t_v
    as 
      select 年级 , count(distinct 班别)
      from   学生表
      group  by 年级
      

  3.   

    原表中本来在1年级就有2个班级,2的数量并不是二个学生
    我不小心多插入了一条重复的记录,即select '1','3
      

  4.   

    create table tb (年级 varchar(20) ,班别 varchar(20),姓名 varchar(20))
    insert into tb
    select '1','1','zhang'
    union all
    select '1','2','wang'
    union all
    select '1','3','li'
    union all
    select '1','3','li'
    union all
    select '2','1','liang'
    union all
    select '2','2','he'
    union all
    select '2','3','chen'
    CREATE FUNCTION p_getClass(@g int)
    RETURNS INT
    AS
    BEGIN 
    DECLARE @C INT
    SELECT @C=COUNT(DISTINCT 班别 ) FROM TB WHERE 年级=@g 
    RETURN @C
    ENDSELECT dbo.p_getClass(1) AS 一班数 , dbo.p_getClass(2) AS 二班数  , dbo.p_getClass(3) AS 三班数 , dbo.p_getClass(4) AS 四班数 , dbo.p_getClass(5) AS 五班数 , dbo.p_getClass(6) AS 六班数 
      

  5.   

    select 年级,
           [一班数]=sum(case [年级] when 1 then 1 else 0 end),
           [二班数]=sum(case [年级] when 2 then 1 else 0 end),
           [三班数]=sum(case [年级] when 3 then 1 else 0 end),
           [四班数]=sum(case [年级] when 4 then 1 else 0 end),
           [五班数]=sum(case [年级] when 5 then 1 else 0 end),
           [六班数]=sum(case [年级] when 6 then 1 else 0 end)
    from 学生表 
    group by 年级
      

  6.   

    select a.年级,
           [一班数]=sum(distinct(case [班别] when '1' then 1 else 0 end)) ,
           [二班数]=sum(distinct(case [班别] when '2' then 1 else 0 end)),
           [三班数]=sum(distinct(case [班别] when '3' then 1 else 0 end)),
           [四班数]=sum(distinct(case [班别] when '4' then 1 else 0 end)),
           [五班数]=sum(distinct(case [班别] when '5' then 1 else 0 end)),
           [六班数]=sum((case [班别] when '6' then 1 else 0 end))
    from tb a
    group by 年级
      

  7.   

    create view t_v
    as 
    select 一班数 = sum(case 年级 when 2 then 班数 else 0 end),
           二班数 = sum(case 年级 when 2 then 班数 else 0 end),
           三班数 = sum(case 年级 when 3 then 班数 else 0 end),
           四班数 = sum(case 年级 when 4 then 班数 else 0 end),
           五班数 = sum(case 年级 when 5 then 班数 else 0 end),
           六班数 = sum(case 年级 when 6 then 班数 else 0 end)
    from  
    (
      select 年级 , count(distinct 班别) 班数
      from   学生表
      group  by 年级
     )  a
      

  8.   

    修改一下:
    select a.年级,
           [一班数]=sum(distinct(case [班别] when '1' then 1 else 0 end)) ,
           [二班数]=sum(distinct(case [班别] when '2' then 1 else 0 end)),
           [三班数]=sum(distinct(case [班别] when '3' then 1 else 0 end)),
           [四班数]=sum(distinct(case [班别] when '4' then 1 else 0 end)),
           [五班数]=sum(distinct(case [班别] when '5' then 1 else 0 end)),
           [六班数]=sum(distinct(case [班别] when '6' then 1 else 0 end))
    from tb a
    group by 年级
      

  9.   

    还是有点困难,但思想很简单的:
    count(distinct 班别) [when 年级=1]
    count(distinct 班别) [when 年级=2]
    count(distinct 班别) [when 年级=3]
    count(distinct 班别) [when 年级=4]
    count(distinct 班别) [when 年级=5]
    count(distinct 班别) [when 年级=6]但不知道要怎么样实现
      

  10.   

    辛苦了
    不知道能不能把 when 年级=1 这个条件加到 count语句中
      

  11.   

    楼上的求的是每个年级的每个班的人数吧,我的理解是楼主只要每个年级的班级数
    select [一班数]=(select count(*)
                       from a
                       where 年级='1'
                       group by 年级,班别)
    ……
    上面只求了一年级的班级数,不知道行不行,楼主自己试试吧
      

  12.   

    create view result as
    select [一班数]=(select count(*)
                       from a
                       where 年级='1'
                       group by 年级,班别)
           [二班数]=(select count(*)
                       from a
                       where 年级='2'
                       group by 年级,班别)
    ……from ……
      

  13.   

    create view all_Class
    as 
     select 年级,count(distinct 班别) from 学生表 group by 年级
      

  14.   

    正确在此:
    select 年级,
           [一班数]=sum(case [班别] when '1' then 1 else 0 end) ,
           [二班数]=sum(case [班别] when '2' then 1 else 0 end),
           [三班数]=sum(case [班别] when '3' then 1 else 0 end),
           [四班数]=sum(case [班别] when '4' then 1 else 0 end),
           [五班数]=sum(case [班别] when '5' then 1 else 0 end),
           [六班数]=sum(case [班别] when '6' then 1 else 0 end)
    from class
    group by 年级结果:
    一班
    1 3 3 0 0 0 0
    2 2 1 1 0 0 0
    6 1 0 0 0 0 0