还是上次的回复,稍微改了一下:
select '1-2' as 节,max(case substring(timecode,1,1) +substring(timecode,3,1) 
   when 'A0' then 科目
   when 'A1' then 科目+'(单)'
   when 'A2' then 科目+'(双)'
   end) as 星期1,
max(case substring(timecode,1,1) +substring(timecode,3,1)  
   when 'B0' then 科目 
   when 'B1' then 科目+'(单)'
   when 'B2' then 科目+'(双)'
   end) as 星期2,
...
from course
where classId=1
and timecode like '_1%'
union all
select '3-4' as 节,max(case substring(timecode,1,1) +substring(timecode,3,1)
   when 'A0' then 科目
   when 'A1' then 科目+'(单)'
   when 'A2' then 科目+'(双)'
   end) as 星期1,
max(case substring(timecode,1,1) +substring(timecode,3,1) 
   when 'B0' then 科目 
   when 'B1' then 科目+'(单)'
   when 'B2' then 科目+'(双)'
   end) as 星期2,
...
from course
where classId=1
and timecode like '_2%'
union all
....

解决方案 »

  1.   

    用这个可以实现:select 班级=classid,课时=b.name
    ,[星期1]=max(case left(timecode,1) when 'A' then subjectid
    +case right(timecode,1) when '1' then '(单)' else '' end else '' end)
    +max(case when left(timecode,1)='A' and right(timecode,1)='2'
    then char(13)+subjectid+'(双)' else '' end)
    ,[星期2]=max(case left(timecode,1) when 'B' then subjectid
    +case right(timecode,1) when '1' then '(单)' else '' end else '' end)
    +max(case when left(timecode,1)='B' and right(timecode,1)='2'
    then char(13)+subjectid+'(双)' else '' end)
    ,[星期3]=max(case left(timecode,1) when 'C' then subjectid
    +case right(timecode,1) when '1' then '(单)' else '' end else '' end)
    +max(case when left(timecode,1)='C' and right(timecode,1)='2'
    then char(13)+subjectid+'(双)' else '' end)
    ,[星期4]=max(case left(timecode,1) when 'D' then subjectid
    +case right(timecode,1) when '1' then '(单)' else '' end else '' end)
    +max(case when left(timecode,1)='D' and right(timecode,1)='2'
    then char(13)+subjectid+'(双)' else '' end)
    ,[星期5]=max(case left(timecode,1) when 'E' then subjectid
    +case right(timecode,1) when '1' then '(单)' else '' end else '' end)
    +max(case when left(timecode,1)='E' and right(timecode,1)='2'
    then char(13)+subjectid+'(双)' else '' end)
    from course a 
    inner join (
    select id=1,name='第一,二节'
    union all select 2,'第三,四节'
    union all select 3,'第五,六节'
    union all select 4,'第七,八节'
    ) b on substring(a.timecode,2,1)=b.id
    group by a.classid,b.id,b.name,substring(a.timecode,2,1)
    order by 班级,b.id
      

  2.   

    下面是数据测试:--数据测试环境
    declare @course table(classId int,subjectid varchar(10),timecode varchar(6))
    insert into @course
    select 1,'语文','A10'
    union all select 1,'体育','B11'
    union all select 1,'数学','B12'
    union all select 1,'机械','D10'
    union all select 1,'数学','A20'
    union all select 1,'物理','A30'
    union all select 1,'化学','B30'--得到课程表
    select 班级=classid,课时=b.name
    ,[星期1]=max(case left(timecode,1) when 'A' then subjectid
    +case right(timecode,1) when '1' then '(单)' else '' end else '' end)
    +max(case when left(timecode,1)='A' and right(timecode,1)='2'
    then char(13)+subjectid+'(双)' else '' end)
    ,[星期2]=max(case left(timecode,1) when 'B' then subjectid
    +case right(timecode,1) when '1' then '(单)' else '' end else '' end)
    +max(case when left(timecode,1)='B' and right(timecode,1)='2'
    then char(13)+subjectid+'(双)' else '' end)
    ,[星期3]=max(case left(timecode,1) when 'C' then subjectid
    +case right(timecode,1) when '1' then '(单)' else '' end else '' end)
    +max(case when left(timecode,1)='C' and right(timecode,1)='2'
    then char(13)+subjectid+'(双)' else '' end)
    ,[星期4]=max(case left(timecode,1) when 'D' then subjectid
    +case right(timecode,1) when '1' then '(单)' else '' end else '' end)
    +max(case when left(timecode,1)='D' and right(timecode,1)='2'
    then char(13)+subjectid+'(双)' else '' end)
    ,[星期5]=max(case left(timecode,1) when 'E' then subjectid
    +case right(timecode,1) when '1' then '(单)' else '' end else '' end)
    +max(case when left(timecode,1)='E' and right(timecode,1)='2'
    then char(13)+subjectid+'(双)' else '' end)
    from @course a 
    inner join (
    select id=1,name='第一,二节'
    union all select 2,'第三,四节'
    union all select 3,'第五,六节'
    union all select 4,'第七,八节'
    ) b on substring(a.timecode,2,1)=b.id
    group by a.classid,b.id,b.name,substring(a.timecode,2,1)
    order by 班级,b.id