首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从另一个表中获取行数?

如何从另一个表中获取行数?
EN

Stack Overflow用户
提问于 2018-12-10 16:27:49
回答 1查看 122关注 0票数 0

我有一张表(table1),上面有从最高到最低年级的学生名单。我想把他们分成三组。但是每个小组有多少学生?首先我数一数我有多少学生,然后在table1中查找NumberStudent列等于学生总数的行(如果我找到的话)。我取group1的数字,意思是group1的学生人数。学生不应在其他小组中重复。

table2

根据每个小组的人数,包含学生人数。

代码语言:javascript
复制
-------------------------------------------
 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名学生

table1

代码语言:javascript
复制
+----+----------+------------+
| 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有多少学生

代码语言:javascript
复制
select count(Id) as Total from Table1

例如,如果我有5名学生,我会根据Table2的数量将他们分成3组。

Table2

代码语言:javascript
复制
NumberStudent| group1 | group2 | group3 |
-----------------------------------------
      5      |    2   |   2    |   1    |

我有2名Group1学生,2名Group2学生,1名Group3学生

代码语言:javascript
复制
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中找不到

代码语言:javascript
复制
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中找不到

代码语言:javascript
复制
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) 

结果应该是

代码语言:javascript
复制
  1  Henri     20    group1      
  2  Eric      20    group1        
  3  François  20    group2            
  4  Gaston    18    group2           
  5  Bertrand  17    group3 
EN

回答 1

Stack Overflow用户

发布于 2018-12-13 16:39:04

如果我的理解正确,你想把学生分成几组,Marks最高的学生应该去第一组等等,而最后一组的Marks应该是最低的?您似乎使用Table2来查找组大小,但为什么不直接计算它们呢?

我会使用一个子选择来按照Marks对所有学生进行排名,然后将这个分数除以被分割的组大小来生成组号。

我不完全确定正确的Firebird语法(这里没有firebird 3.0 ),但如下所示:

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53709728

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档