I want to write a query that will distribute records from one table to another table that take equally number depending in status.
For example, I have two tables, Students and Teachers, which have different courses (status), and I want to assign Students to each Teacher in different courses.
Students:
name
--------+
Adam |
Bennet |
Elmer |
Justin |
Louis |
Noah |
Jack |
Teachers:
name | coerces
---------+---------
Leo | 1
Kyle | 1
Ethan | 1
Leo | 2
Kyle | 2
Ethan | 2
Notice:
One student cannot have more than one teacher.
Each student in course 1 must have a different teacher in course 2.
Each teacher should have the same number of students as any other teacher...
...Unless the number of students is not an exact multiple of the number of teachers, in which case students must be as evenly divided between teachers as possible. For instance, if we have seven students and three teachers the first two teachers will take two students and the last one will take three students.
Results:
Student | Teacher | coerces
--------+---------+---------
Adam | Leo | 1
Bennet | Leo | 1
Elmer | Kyle | 1
Justin | Kyle | 1
Louis | Ethan | 1
Noah | Ethan | 1
Jack | Ethan | 1
Louis | Leo | 2
Noah | Leo | 2
Jack | Kyle | 2
Adam | Kyle | 2
Bennet | Ethan | 2
Elmer | Ethan | 2
Justin | Ethan | 2
How can I do that in Oracle SQL?