Wrong output in MYSQL -
i have question follows:
the total score of hacker sum of maximum scores of challenges. write query print
hacker_id
, name, , total score of hackers ordered descending score.
if more 1 hacker achieved same total score, sort result ascendinghacker_id
.
exclude hackers total score of 0 result.
2 tables given follows:
table: hackers
======================================== hacker_id: integer (id of hacker) name: string (name of hacker) ========================================
table: submissions
=================================================== submission_id: integer (id of submission) hacker_id: integer (id of hacker) challenge_id: integer (id of challenge) score: integer (score of submission) ===================================================
the mysql query i've written follows:-
select a.hacker_id, a.name, a.total from( select h.hacker_id, h.name, sum(case when s.hacker_id=h.hacker_id s.score else 0 end) total hackers h, submissions s group h.hacker_id, h.name ) a.total>0 order a.total desc, a.hacker_id asc;
i'm getting wrong output though output satisfies rules of order & ommission of 0 scorers required. i'm confused error is. please help!!!
the total score of hacker sum of maximum scores of challenges.
first, need find maximum score hacker, each challenge:
select hacker_id , challenge_id , max(score) max_score submissions group hacker_id , challenge_id
then need sum each hacker:
select hacker_id , sum(max_score) total_score ( select hacker_id , challenge_id , max(score) max_score submissions group hacker_id , challenge_id ) ms group hacker_id
finally apply rest:
exclude hackers
total score
of 0 result.hacker_id
,name
, ,total score
ordered descending
score
, ascendinghacker_id
.
select hacker_id , ( select name hackers h h.hacker_id = ms.hacker_id ) name , sum(max_score) total_score ( select hacker_id , challenge_id , max(score) max_score submissions group hacker_id , challenge_id ) ms group hacker_id having sum(max_score) <> 0 order total_score desc , hacker_id
Comments
Post a Comment