0

我有一张表(table1),列出了从最高年级到最低年级的学生名单。我想把它们分成3组。但是每组有多少学生?首先我计算我有多少学生,然后我在 table1 中查找列NumberStudent等于学生总数的行(如果我找到的话)。我取 group1 的数量,这意味着 group1 中的学生人数。学生不应在其他组中重复。

表 2
包含根据他们的数量在每个组中有多少学生

-------------------------------------------
 NumberStudent| group1 | group2 | group3 |
 -----------------------------------------
       1      |    1   |   0    |   0    |  
       2      |    2   |   0    |   0    |
       3      |    2   |   1    |   0    |
       4      |    2   |   2    |   0    |
       5      |    2   |   2    |   1    |
 -----------------------------------------
  • 对于 5 名学生,group1 = 2 名学生,group2 = 2 名学生和 group3 = 1 名学生
  • 对于 3 名学生,group1 = 2 名学生,group2 = 1 名学生和 group3 = 0 名学生

表格1

+----+----------+------------+
| 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组。

表2

NumberStudent| group1 | group2 | group3 |
-----------------------------------------
      5      |    2   |   2    |   1    |

我在 Group1 有 2 个学生,在 Group2 有 2 个学生,在 Group3 有 1 个学生

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 
4

1 回答 1

0

如果我理解正确,您想将学生分组,最高的学生Marks应该排在第一组,等等。最低的学生应该排Marks在最后一组?您似乎使用 aTable2从本质上查找组大小,但为什么不只计算它们呢?

我将使用子选择对所有学生进行排名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
    ) a
于 2018-12-13T16:39:04.147 回答