我有一张表(table1),上面有从最高到最低年级的学生名单。我想把他们分成三组。但是每个小组有多少学生?首先我数一数我有多少学生,然后在table1中查找NumberStudent列等于学生总数的行(如果我找到的话)。我取group1的数字,意思是group1的学生人数。学生不应在其他小组中重复。
table2
根据每个小组的人数,包含学生人数。
-------------------------------------------
NumberStudent| group1 | group2 | group3 |
-----------------------------------------
1 | 1 | 0 | 0 |
2 | 2 | 0 | 0 |
3 | 2 | 1 | 0 |
4 | 2 | 2 | 0 |
5 | 2 | 2 | 1 |
-----------------------------------------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 |
+----+----------+------------+我想数一数Table1有多少学生
select count(Id) as Total from Table1例如,如果我有5名学生,我会根据Table2的数量将他们分成3组。
Table2
NumberStudent| group1 | group2 | group3 |
-----------------------------------------
5 | 2 | 2 | 1 |我有2名Group1学生,2名Group2学生,1名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有2名学生在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有1名学生,在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) 结果应该是
1 Henri 20 group1
2 Eric 20 group1
3 François 20 group2
4 Gaston 18 group2
5 Bertrand 17 group3 发布于 2018-12-13 16:39:04
如果我的理解正确,你想把学生分成几组,Marks最高的学生应该去第一组等等,而最后一组的Marks应该是最低的?您似乎使用Table2来查找组大小,但为什么不直接计算它们呢?
我会使用一个子选择来按照Marks对所有学生进行排名,然后将这个分数除以被分割的组大小来生成组号。
我不完全确定正确的Firebird语法(这里没有firebird 3.0 ),但如下所示:
declare @MyGroupSize double;
set @MyGroupSize = 5.0;
select
floor(a.RANKNO / @MyGroupSize) as GROUPNO,
(a.RANKNO / @MyGroupSize) as TEST123,
a.RANKNO,
a.id,
a.name,
a.Marks
from
(
SELECT
row_number() over(order by Marks DESC)-1 as RANKNO,
id,
name,
Marks
FROM
Students
) ahttps://stackoverflow.com/questions/53709728
复制相似问题