I have a table (table1) with a list of students from the highest to the lowest grade. I want to divide them in 3 groups. But how many students in each group? First I count how many student I have, then I look in table1 for the row where column NumberStudent
is equal to the total of students if I found. I take the number of group1 which means the number of students in group1. The student should not be repeated in other groups.
table2
contains how many students in each groups according to their number
-------------------------------------------
NumberStudent| group1 | group2 | group3 |
-----------------------------------------
1 | 1 | 0 | 0 |
2 | 2 | 0 | 0 |
3 | 2 | 1 | 0 |
4 | 2 | 2 | 0 |
5 | 2 | 2 | 1 |
-----------------------------------------
- For 5 students, group1 = 2 students, group2 = 2 students and group3 = 1 student
- For 3 students, group1 = 2 students, group2 = 1 student and group3 = 0 students
table1
+----+----------+------------+
| id | name | Marks |
+----+----------+------------+
| 1 | Bertrand | 17 |
| 2 | Charles | 10 |
| 3 | Alex | 12 |
| 4 | David | 11 |
| 5 | Eric | 20 |
| 6 | François | 20 |
| 7 | Gaston | 18 |
| 8 | Henri | 20 |
+----+----------+------------+
I want to count how many students are in Table1
select count(Id) as Total from Table1
For example, if I have 5 students, I divide them into 3 groups according to the number of Table2.
Table2
NumberStudent| group1 | group2 | group3 |
-----------------------------------------
5 | 2 | 2 | 1 |
I have 2 students in Group1, 2 students in Group2, 1 student in Group3
select name,Marks from Table1
where Marks >=10
order by Marks row //from table2 (how can i obtain the number row =2 as parameter )
Group2 has 2 students which are not found in group1
select name,Marks from Table1
where Marks >=15 and id<> id // the student in groupe2 not mention in group1
order by Marks rowrow //from table1 (group2 for 5 student is 2 so row =2)
Group3 has 1 student which is not found in group1
select name,Marks from Table1
where Marks >17 and id<> id // the student in groupe3 not mention in group1 and group2
order by Marks row row //from table1 (group3 for 5 student is 1 so row =1)
The result should be
1 Henri 20 group1
2 Eric 20 group1
3 François 20 group2
4 Gaston 18 group2
5 Bertrand 17 group3