We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- SQL
- Basic Join
- Challenges
- Discussions
Challenges
Challenges
Sort by
recency
|
2538 Discussions
|
Please Login in order to post a comment
fun challenge! I created two derived tables, the first one called L1 where I count the number of challenges created by each hacker and the dense rank -important not to use simply rank- of each one, and I use this as a subquery in the FROM clause to create L2 where I count how many times each rank appears (that is, how many times a number of challenges created by a hacker is repeated) in L1. afterwards, I just exclude every record with a repeated number of challenges created by an individual (l2.filter = 1) with the exception of the highest possible number of ranks (l2.rankings = 1)
MYSQL:
MS SQL code
with cc as ( select h.hacker_id,h.name,count(ch.challenge_id) as cnt from hackers h inner join challenges ch on h.hacker_id=ch.hacker_id group by h.hacker_id,h.name) ,cou as (select cnt from cc group by cnt having count(cnt)>1) ,notuniq as (select * from cc where cc.cnt =(select cnt from cou where cou.cnt=(Select max(cnt) from cc))) ,final as (select * from cc where cc.cnt not in (select cnt from cou) union select * from notuniq ) select * from final order by cnt desc,hacker_id
WITH CHALLENGE AS ( select h.hacker_id,name, count(challenge_id) as n_challenges from hackers h join challenges c on h.hacker_id = c.hacker_id group by 1,2 order by count(challenge_id) DESC ), less_max as( select hacker_id, name, n_challenges from challenge WHERE n_challenges < ( select max(n_challenges) from challenge ) ) select * from challenge where n_challenges = ( select max(n_challenges) from challenge ) union all select * from less_max where n_challenges NOT IN ( SELECT n_challenges FROM less_max GROUP BY n_challenges HAVING count(*) > 1 ) order by n_challenges desc, hacker_id asc
I solved with this approach took me 20 mins to solve
Thanks for saying how long it took you, I was wondering how hard other people felt this was. It probably took me about the same amount of time. My solution was a little different from yours, but got to the same result.
WITH CTE AS( SELECT C.hacker_id, name, COUNT(challenge_id) AS Total_Challenges FROM Challenges AS C JOIN Hackers AS H ON C.hacker_id = H.hacker_id GROUP BY C.hacker_id,name ), CTE1 AS( SELECT Total_Challenges,COUNT() AS Cnt FROM CTE WHERE Total_Challenges <> (SELECT MAX(Total_Challenges) FROM CTE) GROUP BY Total_Challenges HAVING COUNT()>1 ) SELECT * FROM CTE WHERE Total_Challenges NOT IN (SELECT DISTINCT(Total_Challenges) FROM CTE1) ORDER BY Total_Challenges DESC,hacker_id ASC