Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql SQL查询,列出2020年至少有一名学生排名在1和3之间的所有学院_Mysql_Sql_Database - Fatal编程技术网

Mysql SQL查询,列出2020年至少有一名学生排名在1和3之间的所有学院

Mysql SQL查询,列出2020年至少有一名学生排名在1和3之间的所有学院,mysql,sql,database,Mysql,Sql,Database,在每年的无人机锦标赛上,来自不同学院的学生 参与并赢得排名。给定图像中的表结构 下面, 请编写SQL查询,列出2020年至少有一名学生排名在1和3之间的所有学院 预期产出:学院名称、2020年最佳学生排名、2020年排名b/w 1和3的学生人数 我已经试过了,但是没有得到正确的答案, 请帮忙 SELECT Colleges.Name, RANK() OVER(ORDER BY Rankings.ranking DESC) as "Best Ranking&qu

在每年的无人机锦标赛上,来自不同学院的学生 参与并赢得排名。给定图像中的表结构 下面,

请编写SQL查询,列出2020年至少有一名学生排名在1和3之间的所有学院

预期产出:学院名称、2020年最佳学生排名、2020年排名b/w 1和3的学生人数

我已经试过了,但是没有得到正确的答案, 请帮忙

SELECT 
    Colleges.Name,  
    RANK() OVER(ORDER BY Rankings.ranking DESC)   as "Best Ranking",
    RANK() OVER (PARTITION BY Colleges.ID ORDER BY Rankings.ranking) as "Total Students in Ranking"
from
    Colleges
    JOIN Students on Students.collegeID = Colleges.ID
    JOIN Rankings on Students.ID = Rankings.studentID
where
    Rankings.year = 2020
    and Rankings.ranking >= 3;

如果我理解正确,这就是您需要做的:

列出2020年至少有一名学生排名在1-3之间的所有学院

要回答您的新问题:

学院名称、2020年最佳学生排名、2020年排名b/w 1和3的学生人数


标记您的数据库,您正在使用哪些dbms?还应提供样本数据和所需信息output@AkashNikam . . . 此信息属于问题,不属于评论。这回答了您提出的问题,我建议您接受此答案。@eshirvana&@gordonlinoff,我已更新了我的问题,请过目。@AkashNikam,您提出了一个新问题,请参阅更新的答案
SELECT 
    Colleges.Name,  
    RANK() OVER(ORDER BY Rankings.ranking DESC)   as "Best Ranking",
    RANK() OVER (PARTITION BY Colleges.ID ORDER BY Rankings.ranking) as "Total Students in Ranking"
from
    Colleges
    JOIN Students on Students.collegeID = Colleges.ID
    JOIN Rankings on Students.ID = Rankings.studentID
where
    Rankings.year = 2020
    and Rankings.ranking >= 3;
select * from colleges c
where exists ( select 1 from students s
                join Rankings r on s.ID = r.studentID 
                and s.collegeID = c.ID 
                and r.ranking <= 3
                and r.year = 2020
)
select 
    c.Name   
    , min(ranking) 
    , count(*)
from colleges c
join students s 
   on s.collegeID = c.ID 
join Rankings r 
   on s.ID = r.studentID 
   and r.ranking <= 3
   and r.year = 2020
group by c.Name