小编典典

查询总分

sql

输入表: 提交

提交包含四列,Contestant_id表示针对不同问题提交的不同参赛者的ID。一个竞赛者可以针对一个问题提交多次以上。因此,针对Contestant_id的challenge_id可能会出现多次。

 submission_id  Contestant_id challenge_id    score
    11                 1             333        90
    22                 2             333        60
    33                 3             333        80
    44                 4             333         0
    112                1             333        45
    113                1             444        80
    114                2             444        70

产出表: 总分

Contestant_id     score 
    1              170
    2              130
    3               80

在这里,我们将总分视为-

 for contestant_id 1 :  total score = max(90,45)+ 80 = 170
 for contestant_id 2 :  total score = 60 + 70 = 130
 for contestant_id 3 :  total score = 80 
 for contestant_id 4 :  total score = 0 ;so we exclude it

要取得总分,我必须最多获得一个参赛者ID,如果有多个相同的Challenge_ID,则总和不等于总和。但是,我坚持这样做,该怎么做。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

选择Contestant_id,得分为(选择总和(max(Select ....


阅读 237

收藏
2021-03-08

共1个答案

小编典典

您似乎需要两个聚合级别。您需要每个参赛者和挑战的最高分数。然后,您要将它们加起来。

这是一种方法:

select Contestant_id, sum(max_score)
from (select Contestant_id, challenge_id, max(score) as max_score
      from Submissions
      group by Contestant_id, challenge_id,
     ) t
group by Contestant_id;

如果要创建输出表,可以在into total_scores后面添加select

2021-03-08