select 教学内容,周次,评分 as 一月,null as 二月 from myTable where 教师姓名='张三' and 年月='200901' and 教学内容='备课' union all select 教学内容,周次,null as 一月,评分 as 二月 from myTable where 教师姓名='张三' and 年月='200902' and 教学内容='备课' 
这是我做的仅一二月份的备课样式,但是二月的排不到一月后面,而是重新生成一行

解决方案 »

  1.   

    要写应用程序的,你的需求不是用一个SQL语句能完成的。
      

  2.   

    declare @t table (教师姓名 varchar(10),教学内容 varchar(10),年月 varchar(10),周 int,评分 int)
    insert @t select '张三','备课','200901',1,50
    insert @t select '张三','备课','200901',2,60
    insert @t select '张三','备课','200901',3,50
    insert @t select '张三','备课','200901',4,40
    insert @t select '张三','上课','200901',1,30
    insert @t select '张三','上课','200901',2,20
    insert @t select '张三','备课','200902',1,80
    insert @t select '张三','备课','200902',2,90
    insert @t select '张三','备课','200903',1,60
    --select * from @t
    select * from (
    select
       教学内容 内容, 
       周 周次,
       max(case when cast(right(年月,2) as int)=1 then 评分 else 0 end) 一月,
       max(case when cast(right(年月,2) as int)=2 then 评分 else 0 end) 二月,
       max(case when cast(right(年月,2) as int)=3 then 评分 else 0 end) 三月
    from @t
    group by 教学内容,周
    union all 
    select 
      教学内容+'周合计',
      0,
      sum(case when cast(right(年月,2) as int)=1 then 评分 else 0 end) 一月,
      sum(case when cast(right(年月,2) as int)=2 then 评分 else 0 end) 二月,
      sum(case when cast(right(年月,2) as int)=3 then 评分 else 0 end) 三月
    from @t
    group by 教学内容
    union all 
    select 
      '总合计',
      0,
      sum(case when cast(right(年月,2) as int)=1 then 评分 else 0 end) 一月,
      sum(case when cast(right(年月,2) as int)=2 then 评分 else 0 end) 二月,
      sum(case when cast(right(年月,2) as int)=3 then 评分 else 0 end) 三月
    from @t ) t
    order by 内容,周次内容               周次          一月          二月          三月
    ---------------- ----------- ----------- ----------- -----------
    备课               1           50          80          60
    备课               2           60          90          0
    备课               3           50          0           0
    备课               4           40          0           0
    备课周合计            0           200         170         60
    上课               1           30          0           0
    上课               2           20          0           0
    上课周合计            0           50          0           0
    总合计              0           250         170         60(9 行受影响)
      

  3.   

    create table tb(教师姓名 varchar(10),教学内容 varchar(10),年月 varchar(10),周 int,评分 int)
    insert tb select '张三','备课','200901',1,50
    insert tb select '张三','备课','200901',2,60
    insert tb select '张三','备课','200901',3,50
    insert tb select '张三','备课','200901',4,40
    insert tb select '张三','上课','200901',1,30
    insert tb select '张三','上课','200901',2,20
    insert tb select '张三','备课','200902',1,80
    insert tb select '张三','备课','200902',2,90
    insert tb select '张三','备课','200903',1,60
    go
    select * from
    (
    select 教学内容,cast(周 as varchar(10)) 周次,
      max(case right(年月,2) when '01' then 评分 else 0 end) [01月],
      max(case right(年月,2) when '02' then 评分 else 0 end) [02月],
      max(case right(年月,2) when '03' then 评分 else 0 end) [03月]
    from tb
    group by 教学内容,cast(周 as varchar(10))
    union all
    select 教学内容,周次='合计',
      sum(case right(年月,2) when '01' then 评分 else 0 end) [01月],
      sum(case right(年月,2) when '02' then 评分 else 0 end) [02月],
      sum(case right(年月,2) when '03' then 评分 else 0 end) [03月]
    from tb
    group by 教学内容
    union all
    select 教学内容='总合计',周次='',
      sum(case right(年月,2) when '01' then 评分 else 0 end) [01月],
      sum(case right(年月,2) when '02' then 评分 else 0 end) [02月],
      sum(case right(年月,2) when '03' then 评分 else 0 end) [03月]
    from tb
    ) t
    order by 教学内容,周次drop table tb/*
    教学内容       周次         01月         02月         03月         
    ---------- ---------- ----------- ----------- ----------- 
    备课         1          50          80          60
    备课         2          60          90          0
    备课         3          50          0           0
    备课         4          40          0           0
    备课         合计         200         170         60
    上课         1          30          0           0
    上课         2          20          0           0
    上课         合计         50          0           0
    总合计                   250         170         60(所影响的行数为 9 行)
    */
      

  4.   

    谢谢各位,我用的是c# ,数据库是access
      

  5.   

    我最后用子查询办法做了,句子太长,如下:
    select a.教学内容,a.周,a.评分 as 一月,(select b.评分 from 数据表 as b where b.教师姓名='" + comboBox2.Text.Trim() + "' and b.年月='200902' and b.教学内容=a.教学内容 and b.周=a.周) as 二月,(select c.评分 from 数据表 as c where c.教师姓名='" + comboBox2.Text.Trim() + "' and c.年月='200903' and c.教学内容=a.教学内容 and c.周=a.周) as 三月,... from 数据表 as a where a.教师姓名='" + comboBox2.Text.Trim() + "' and a.年月='200901' order by a.教学内容,a.周
    一直写到十二月,麻烦了点,但实现了基本的功能,但问题还有两个
    1\周合计出不来,我指用这个方法
    2\月合计有空值的就出错,如何把空设为0
      

  6.   

    有一定难度,是3个sql记录集的集合
      

  7.   

    create table tb(教师姓名 varchar(10),教学内容 varchar(10),年月 varchar(10),周 int,评分 int)
    insert tb select '张三','备课','200901',1,50
    insert tb select '张三','备课','200901',2,60
    insert tb select '张三','备课','200901',3,50
    insert tb select '张三','备课','200901',4,40
    insert tb select '张三','上课','200901',1,30
    insert tb select '张三','上课','200901',2,20
    insert tb select '张三','备课','200902',1,80
    insert tb select '张三','备课','200902',2,90
    insert tb select '张三','备课','200903',1,60
    go
    select * from
    (
    select 教学内容,cast(周 as varchar(10)) 周次,
      max(case right(年月,2) when '01' then 评分 else 0 end) [01月],
      max(case right(年月,2) when '02' then 评分 else 0 end) [02月],
      max(case right(年月,2) when '03' then 评分 else 0 end) [03月]
    from tb
    group by 教学内容,cast(周 as varchar(10))
    union all
    select 教学内容,周次='合计',
      sum(case right(年月,2) when '01' then 评分 else 0 end) [01月],
      sum(case right(年月,2) when '02' then 评分 else 0 end) [02月],
      sum(case right(年月,2) when '03' then 评分 else 0 end) [03月]
    from tb
    group by 教学内容
    union all
    select 教学内容='总合计',周次='',
      sum(case right(年月,2) when '01' then 评分 else 0 end) [01月],
      sum(case right(年月,2) when '02' then 评分 else 0 end) [02月],
      sum(case right(年月,2) when '03' then 评分 else 0 end) [03月]
    from tb
    ) t
    order by 教学内容,周次
      

  8.   

    谢谢各位,可我用access做的,有办法吗?
      

  9.   

    把case when then end 换用 iif 试一下.
      

  10.   


    if object_id('tb') is not null drop table tb
    go
    create table tb(教师姓名 varchar(10),教学内容 varchar(10),年月 varchar(10),周 int,评分 int)
    insert tb select '张三','备课','200901',1,50
    insert tb select '张三','备课','200901',2,60
    insert tb select '张三','备课','200901',3,50
    insert tb select '张三','备课','200901',4,40
    insert tb select '张三','上课','200901',1,30
    insert tb select '张三','上课','200901',2,20
    insert tb select '张三','备课','200902',1,80
    insert tb select '张三','备课','200902',2,90
    insert tb select '张三','备课','200903',1,60
    go
    select * from 
    (
    select 
    case when 教学内容 is null then '总计' else 教学内容 end [教学内容],
    周,
    max(case when right(年月,2)='01' then 评分 else 0 end) [一月],
    max(case when right(年月,2)='02' then 评分 else 0 end) [二月],
    max(case when right(年月,2)='03' then 评分 else 0 end) [三月]
    from tb group  by 教学内容,周
    union all
    select 
    case when 教学内容 is null then '总计' else 教学内容+'合计' end [教学内容],
    周='',
    sum(case when right(年月,2)='01' then 评分 else 0 end) [一月],
    sum(case when right(年月,2)='02' then 评分 else 0 end) [二月],
    sum(case when right(年月,2)='03' then 评分 else 0 end) [三月]
     from tb group  by 教学内容 with rollup
    )a
    order by 教学内容
      

  11.   

    if object_id('tb') is not null drop table tb
    go
    create table tb(教师姓名 varchar(10),教学内容 varchar(10),年月 varchar(10),周 int,评分 int)
    insert tb select '张三','备课','200901',1,50
    insert tb select '张三','备课','200901',2,60
    insert tb select '张三','备课','200901',3,50
    insert tb select '张三','备课','200901',4,40
    insert tb select '张三','上课','200901',1,30
    insert tb select '张三','上课','200901',2,20
    insert tb select '张三','备课','200902',1,80
    insert tb select '张三','备课','200902',2,90
    insert tb select '张三','备课','200903',1,60
    goselect 
    case when 教学内容 is null then '总计' when 教学内容 is not null and 周 is null then 教学内容+'合计' else 教学内容 end [教学内容],
    周 ,
    sum(case when right(年月,2)='01' then 评分 else 0 end) [一月],
    sum(case when right(年月,2)='02' then 评分 else 0 end) [二月],
    sum(case when right(年月,2)='03' then 评分 else 0 end) [三月]
    from tb group  by 教学内容,周 with rollup
      

  12.   

    select * from
    (
    select 教学内容,cast(周 as varchar(10)) 周次,
      max(iif (right(年月,2)='01' ,评分 ,0)) [01月],
      max(iif (right(年月,2)='02' ,评分 ,0)) [02月],
      max(iif (right(年月,2)='03' ,评分 ,0)) [03月]
    from tb
    group by 教学内容,cast(周 as varchar(10))
    union all
    select 教学内容,周次='合计',
      sum(iif (right(年月,2)='01' ,评分 ,0)) [01月],
      sum(iif (right(年月,2)='02' ,评分 ,0)) [02月],
      sum(iif (right(年月,2)='03' ,评分 ,0)) [03月]
    from tb
    group by 教学内容
    union all
    select 教学内容='总合计',周次='',
      sum(iif (right(年月,2)='01' ,评分 ,0)) [01月],
      sum(iif (right(年月,2)='02' ,评分 ,0)) [02月],
      sum(iif (right(年月,2)='03' ,评分 ,0)) [03月]
    from tb
    ) t
    order by 教学内容,周次
      

  13.   

    楼主朋友,如果是用的access数据库,那么T-SQL就不怎么支持了。只能从数据表里读一条记录,按照自己设定好的格式填充到一个DataTable对象里了。