课程表的SQL语句(再问)

zitjubiz 2003-09-22 03:47:45
本来已经问过一次了,
http://expert.csdn.net/Expert/TopicView1.asp?id=2275672
但单双周问题没有解决

course表
classId 班级
subjectid 科目
timecode 时间代码(如A20)
一天一共有8节,每个科目连上2节,一个科目占学期的教学周数
0表示所有周,1为单周,2为双周,ABCDEFG表示星期几
例如:
星期1 星期2 星期3 星期4 星期5
第一,二节 语文 体育(单) 机械
数学(双)
第三,四节 数学

第五,六节 物理 化学

第七,八节

怎样写SQL语句可以排出课程表的样子
横行是星期1-7
竖行是节
...全文
98 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 元老 2003-09-24
  • 打赏
  • 举报
回复
下面是数据测试:

--数据测试环境
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
zjcxc 元老 2003-09-24
  • 打赏
  • 举报
回复
用这个可以实现:

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
Rewiah 2003-09-24
  • 打赏
  • 举报
回复
一次只能查一个班级的,因为不同班级的课程表应该分开
txlicenhe 2003-09-24
  • 打赏
  • 举报
回复
有一定难度。
Rewiah 2003-09-24
  • 打赏
  • 举报
回复
还是上次的回复,稍微改了一下:


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
....


zitjubiz 2003-09-24
  • 打赏
  • 举报
回复
单双周的问题怎么解决啊?
zitjubiz 2003-09-22
  • 打赏
  • 举报
回复
我是想手工排课,我是想产生一个这样的表给用户,比较直观。
yujohny 2003-09-22
  • 打赏
  • 举报
回复
你的单双周问题想怎么解决,
是给定时间,你排课程表
还是产生一个表,像你这样的例子

星期1 星期2 星期3 星期4 星期5
第一,二节 语文 体育(单) 机械
数学(双)
第三,四节 数学

第五,六节 物理 化学

第七,八节

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧