使用SQL的每周计划

时间:2019-05-09 12:44:57

标签: sql oracle plsql oracle11g

我需要一个每周的学生时间表。

  • 行是12小时(08:00-20:00),
  • 列为6天(周一至周六)。
  • 学生在周一的9-11点至11-14周二之间开设课程。
  • 其他将显示为空。

我可以仅使用SQL创建类似的查询(矩阵)吗?

我当前的查询是这样的

SELECT courseName, courseDay, courseStartHour, coursEndHour 
FROM courses WHERE studentId = 1

这就是我想要的:

enter image description here

我在Pivot上有这个功能

enter image description here

同一小时可以包含多个课程。 (最多2个)

注意:我们有Oracle 11g。

样本(简化)数据:

CREATE TABLE StudentCourses ( courseCode varchar2(8) NOT NULL,  courseName varchar2(64) NOT NULL,  day number(10),  startHour number(10),  endHour number(10));

INSERT INTO StudentCourses (courseCode, courseName, day, startHour, endHour ) VALUES ( 'ADV 352','Advertising Copywriting','1','9','11' );
INSERT INTO StudentCourses (courseCode, courseName, day, startHour, endHour ) VALUES ( 'ADV 352','Advertising Copywriting','1','11','13' );
INSERT INTO StudentCourses (courseCode, courseName, day, startHour, endHour ) VALUES ( 'ADV 332','Positioning Strategy in Advertising','2','9','12' );
INSERT INTO StudentCourses (courseCode, courseName, day, startHour, endHour ) VALUES ( 'COMM 324','Persuasion and Perception','2','14', '17' );
INSERT INTO StudentCourses (courseCode, courseName, day, startHour, endHour ) VALUES ( 'ADV 312','Corporate Communications Practicum','3','14','17' );
INSERT INTO StudentCourses (courseCode, courseName, day, startHour, endHour ) VALUES ( 'ADV 302','Strategic Media Planning','4','9','11' );
INSERT INTO StudentCourses (courseCode, courseName, day, startHour, endHour ) VALUES ( 'ADV 302','Strategic Media Planning','4','11','13' );
INSERT INTO StudentCourses (courseCode, courseName, day, startHour, endHour ) VALUES ( 'ADV 412','Case Studies in Advertising','4','13','15' );
INSERT INTO StudentCourses (courseCode, courseName, day, startHour, endHour ) VALUES ( 'ADV 411','Advertising Photography','4','14','16' );
INSERT INTO StudentCourses (courseCode, courseName, day, startHour, endHour ) VALUES ( 'ADV 412','Case Studies in Advertising','4','15','17' );
INSERT INTO StudentCourses (courseCode, courseName, day, startHour, endHour ) VALUES ( 'ADV 411','Advertising Photography','4','16','18' );

2 个答案:

答案 0 :(得分:2)

这是一个简单的。

WITH TEMP AS(SELECT LEVEL+7 AS HR  FROM DUAL CONNECT BY LEVEL <= 12)
SELECT TEMP.HR AS "HOUR",
       CASE WHEN TEMP.HR BETWEEN 9 AND 11 THEN 'X' ELSE NULL END AS MON,
       CASE WHEN TEMP.HR BETWEEN 11 AND 14 THEN 'X' ELSE NULL END AS TUE,
       NULL AS WED,
       NULL AS THU,
       NULL AS FRI,
       NULL AS SAT,
       NULL AS SUN
       FROM DUAL, TEMP

答案 1 :(得分:1)

这就是您需要的吗?

with tab as (
select 8 as "hour", null as monday, null as tuesday, null as wednesday, null as thursday, null as friday, null as saturday from dual union all
select 9 as "hour", 'x' as monday, null as tuesday, null as wednesday, null as thursday, null as friday, null as saturday  from dual union all
select 10 as "hour", 'x' as monday, null as tuesday, null as wednesday, null as thursday, null as friday, null as saturday  from dual union all
select 11 as "hour", 'x' as monday, 'x' as tuesday, null as wednesday, null as thursday, null as friday, null as saturday  from dual union all
select 12 as "hour", null as monday, 'x' as tuesday, null as wednesday, null as thursday, null as friday, null as saturday  from dual union all
select 13 as "hour", null as monday, 'x' as tuesday, null as wednesday, null as thursday, null as friday, null as saturday  from dual union all
select 14 as "hour", null as monday, 'x' as tuesday, null as wednesday, null as thursday, null as friday, null as saturday  from dual union all
select 15 as "hour", null as monday, null as tuesday, null as wednesday, null as thursday, null as friday, null as saturday  from dual union all
select 16 as "hour", null as monday, null as tuesday, null as wednesday, null as thursday, null as friday, null as saturday  from dual union all
select 17 as "hour", null as monday, null as tuesday, null as wednesday, null as thursday, null as friday, null as saturday  from dual union all
select 18 as "hour", null as monday, null as tuesday, null as wednesday, null as thursday, null as friday, null as saturday  from dual 
)
select * from tab