以下解决方案的反例?

时间:2018-08-30 18:07:53

标签: sql

我最近通过以下提示完成了编码挑战:

  

黑客的总得分是所有黑客的最高得分之和   挑战。编写查询以打印hacker_id,名称和   黑客的总得分由降序排列。如果更多   而不是一个黑客获得相同的总分,然后对结果进行排序   升序hacker_id。将总得分为0的所有黑客从   您的结果。

     

下表包含比赛数据:

     

黑客:hacker_id是黑客的ID,名称是名称   的黑客。

     

提交:submission_id是提交的ID,hacker_id   是进行提交的黑客的ID,challenge_id是   提交所属挑战的ID,得分为   提交的分数。

我的解决方案通过了测试用例,但是我花了很多次迭代才能达到目标。

我感觉可能存在一个边缘情况/特定输入,使用我的解决方案无法通过,但是我很难弄清楚。

有任何猜想或反例吗?

我的解决方案:

Select ID, Name, sum(maxscore) as tot From
(Select ID, Name, chal, Max(score) as maxscore From
(Select Submissions.hacker_id as ID, Hackers.name as Name, Submissions.score as score, Submissions.challenge_id as chal
From Submissions
    Inner Join Hackers on Submissions.hacker_id = Hackers.hacker_id
    Where Submissions.score <> 0)
Group by chal, ID, Name)
Group by ID, Name Order by tot desc, ID asc;

4 个答案:

答案 0 :(得分:0)

Select a.hacker_id,a.name,x.sum_max
from Hackers a
inner join 
(SELECT hacker_id,sum(max_score) as sum_max from (
Select hacker_id,challenge_id,max(Score) as max_score
from submissions
group by hacker_id,challenge_id)b 
group by hacker_id
having sum_max>0)x on a.hacker_id=x.hacker_id
order by sum_max desc,hacker_id

答案 1 :(得分:0)

这是我的

select T.hacker_id, T.name , sum(T.max_score) sum_score from (
Select h.hacker_id, name , challenge_id, max(score) max_score from Hackers h 
inner join submissions s on h.hacker_id = s.hacker_id
group by h.hacker_id, name , challenge_id) as T
group by T.hacker_id, T.name
having sum(T.max_score) > 0
order by sum_score desc , T.hacker_id

答案 2 :(得分:0)

MySQL 的这段代码成功运行了测试用例。

select m.id, h.name, m.total from 
    (select x.hacker_id id, sum(x.max_score) total from
        (select h.hacker_id, s.challenge_id, max(s.score) as max_score from submissions as s 
        join hackers as h on h.hacker_id = s.hacker_id
        group by h.hacker_id, s.challenge_id) as x
    group by x.hacker_id
    having total <> 0)as m, 
hackers as h
where m.id = h.hacker_id
order by m.total desc, m.id;

enter image description here

答案 3 :(得分:-2)

我尝试了这个并与我一起工作
选择 H.Hacker_id, h.Name, sum(x.Score) 来自黑客 h 内连接 (
选择hacker_id、challenge_id、max(Score)作为Score 从提交 按hacker_id、challenge_id分组 ) X H.Hacker_id = x.hacker_Id 分组 H.Hacker_Id, h.Name 总和(x.score)> 0 按 3 desc 排序,h.hacker_id