我的数据如下
Schedule
+------------+----------------+
| Date | Lesson Teacher |
+------------+----------------+
| 2018-01-03 | Math |
| 2018-01-03 | English |
| 2018-01-03 | Sports |
| 2018-01-03 | Biology |
| 2018-01-03 | Math |
| 2018-01-03 | English |
| 2018-01-03 | Sports |
| 2018-01-03 | Biology |
| 2018-01-04 | Math |
| 2018-01-04 | English |
| 2018-01-04 | Sports |
| 2018-01-04 | Biology |
+------------+----------------+
Teacher
+-------------+------------+-----------+
| Lesson | Teacher | Priority |
+-------------+------------+-----------+
| Math | Alex | 1 |
| Math | Bob | 2 |
| Math | Steve | 3 |
| English | Alex | 1 |
| English | Bertha | 2 |
| Sports | Noah | 1 |
| Sports | Steve | 2 |
| Biology | Bertha | 1 |
| Biology | Bob | 2 |
+-------------+------------+-----------+
每位教师只能在一个日期教授1节课,所以如果时间表已经填满了第一优先级教师,那么下一课将在课程安排的同一日期填写第二课。有人可以帮忙吗?
THX
答案 0 :(得分:0)
我会使用windows函数
select sch.date, sch.lesson, t.teacher
from teacher t
join
(
select s.*,
row_number() over (partition by date, lesson order by date) priority
from schedule s
) sch on sch.lesson = t.lesson and t.priority = sch.priority