Еженедельное расписание с SQL - PullRequest
0 голосов
/ 09 мая 2019

Мне нужен еженедельный планировщик для студентов.

  • Строки - 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 ]

2 голосов
/ 09 мая 2019

Вот более простой вариант.

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 голос
/ 09 мая 2019

это то, что вам нужно?

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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...