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.
15 Days of Learning SQL
15 Days of Learning SQL
Sort by
recency
|
1117 Discussions
|
Please Login in order to post a comment
Good solution using recursion:
-- Using MS SQL Server
-- get daily submissions per hacker with cte as ( SELECT submission_date, hacker_id, count(distinct submission_id) as submissions FROM submissions GROUP BY submission_date, hacker_id )
-- get submission days per hacker and rank the hackers by submissions made each day in descending order , cte2 as ( SELECT submission_date, hacker_id, submissions, count(submission_date) OVER (PARTITION BY hacker_id order by submission_date rows between unbounded preceding and current row) AS submission_days, datediff(day, cast('2016-03-01' as date), submission_date) + 1 as days_elapsed, rank() over (partition by submission_date order by submissions desc, hacker_id) as max_submissions_rank FROM cte )
-- get the number of users who have been submitting daily since the start of the challenge and the hackerid with the maximum submissions per day , cte3 as ( select submission_date, count(distinct case when days_elapsed = submission_days then hacker_id end) as daily_submit_hacker_cnt, max(case when max_submissions_rank = 1 then hacker_id end) as max_submissions_hacker_id from cte2 group by submission_date )
-- get the name of the hacker who has maximum submissions per day select submission_date, daily_submit_hacker_cnt, max_submissions_hacker_id, name as max_submissions_hacker_name from cte3 left join hackers on cte3.max_submissions_hacker_id = hackers.hacker_id order by submission_date;
With MySql, window functions do not work in this one. Am I right?. I could not make them work in this one.
This one was difficult. I found a discussion that answered a lot of problems I had with this one. This is what they are really looking for. pretty easy except for the "tracking the people that did it everyday" part. I had to look for help and then ended up creating a buffer to count the everyday people. I had to look at it several times to figure out a workable solution. You have to iterate every day sequentially, and update the everyday people buffer. Once you can nest that in there, it's not so hard.
"select Submission_date, Number of hackers who have submitted every day so far, Hacker_id of the hacker with the most submits on this day, (regardless of which other days they submitted) Their_name