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
|
1089 Discussions
|
Please Login in order to post a comment
simple, clean solution:
What is wrong with the following?
with cte as( select submission_date, hacker_id, count(*) as no_of_submissions, dense_rank() over(partition by submission_date order by submission_date) as day_number from Submissions group by submission_date, hacker_id), cte2 as( select , count() over(partition by hacker_id order by submission_date) as till_date_submission, case when day_number = count(*) over(partition by hacker_id order by submission_date) then 1 else 0 end as unique_flag from cte ),cte3 as( select *, sum(unique_flag) over(partition by submission_date) as unique_count, row_number() over(partition by submission_date order by no_of_submissions desc, hacker_id) as rn from cte2 ) select submission_date,unique_count,c.hacker_id,name from cte3 c join Hackers h on c.hacker_id = h.hacker_id where rn = 1 order by submission_date;
What is wrong with the following?
whats wrong in this?
With daily_submission AS (select submission_date,hacker_id,COUNT(*) as submissions from Submissions where submission_date BETWEEN '2016-03-01' AND '2016-03-15' GROUP BY submission_date,hacker_id), Daily_top_hacker AS (select submission_date,hacker_id,RANK()OVER(PARTITION BY submission_date ORDER BY submissions DESC,hacker_id) AS rank FROM daily_submission) SELECT dts.submission_date, COUNT(DISTINCT dts.hacker_id) AS unq_hackers, dth.hacker_id, h.name from daily_submission dts INNER JOIN Daily_top_hacker dth ON dts.submission_date = dth.submission_date AND dts.hacker_id=dth.hacker_id AND dth.rank=1 INNER JOIN Hackers h ON dth.hacker_id=h.hacker_id GROUP BY dts.submission_date,dth.hacker_id,h.name ORDER BY dts.submission_date;