Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.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
Sql 将分数相加并按数字排列_Sql - Fatal编程技术网

Sql 将分数相加并按数字排列

Sql 将分数相加并按数字排列,sql,Sql,我有一张有这些字段的学生表 student continent marks john Asia 500 jack south America 600 Amy Africa 450 Olive Africa 600 Jay Africa 755 Mackey Asia 699 James Europe

我有一张有这些字段的学生表

student  continent        marks
john      Asia             500
jack      south America    600
Amy       Africa           450
Olive     Africa           600
Jay       Africa           755
Mackey     Asia            699
James     Europe           823
我已经厌倦了通过下面的代码对分数进行求和来打印大陆和总分数-

SELECT  continent, sum(marks) as Totalmarks 
from students 
group by continent 
order by Totalmarks desc;
我应该如何对它们进行排序并打印出低于预期的输出-

continent  Totalmarks  Rank

您可以使用
行号
。试试看:

     SELECT continent
            ,sum(marks) AS Totalmarks
            ,ROW_NUMBER() OVER (
                ORDER BY Totalmarks
                ) AS rank
        FROM students
        GROUP BY continent
        ORDER BY Totalmarks DESC
您可以使用
rank()
densite\u rank()
,具体取决于您希望如何处理领带。对于聚合,这看起来像:

SELECT continent, sum(marks) as Totalmarks,
       dense_rank() over (order by sum(marks) desc) as the_rank
from students 
group by continent 
order by Totalmarks desc;
我怀疑你想要
densite\u rank()
所以:

total   rank dense_rank
 100      1      1
 100      1      1
  90      3      2

如果要将相同的排名分配给平局,请在下面的查询中使用
densite\u RANK
而不是
ROW\u NUMBER()

SELECT 
continent,
SUM(marks) AS Totalmarks,
ROW_NUMBER() OVER (ORDER BY SUM(marks)) AS rank
FROM students
GROUP BY continent
ORDER BY Totalmarks DESC

下面是所需的输出???我的意思是我需要按上述格式输出。对不起,您提供的信息不足以作为所需数据集的详细信息。你可以像你分享的表格内容一样详细阐述。詹姆斯和杰克可能会觉得
SUM()
不公平。如果您想比较学生的成绩,为什么不使用
AVG()
。我只需要查询将输出打印为大陆|总体分数|排名亚洲| 2022 | 1非洲| 1805 | 2南美洲| 850 | 3个分数,但我想按行数对它们进行排名。得到了我需要的:)