15 Days of Learning SQL

Sort by

recency

|

1099 Discussions

|

  • + 0 comments

    -- submissions_per_day with hacker_submissions as (select hacker_id, submission_date,count(submission_id) submission_cnt from submissions group by hacker_id, submission_date),

    -- rank hackers on submission_cnt ranked_hackers as (select hs.submission_date,hs.hacker_id, submission_cnt, row_number() over (partition by hs.submission_date order by submission_cnt desc, hs.hacker_id asc) rnk from hacker_submissions hs),

    -- eligible hackers cte as (select hacker_id, submission_date, count(submission_cnt) over (partition by hacker_id order by submission_date) date_before_submissions from hacker_submissions), eligible_hackers as (select hacker_id,submission_date from cte where day(submission_date) = date_before_submissions),

    -- unique eligible hackers count unique_hackers as (select submission_date, count(hacker_id) unique_cnt from eligible_hackers group by submission_date)

    --combine tables select rh.submission_date,uh.unique_cnt,rh.hacker_id,h.name from ranked_hackers rh inner join unique_hackers uh on uh.submission_date=rh.submission_date inner join hackers h on h.hacker_id = rh.hacker_id where rh.rnk=1 order by rh.submission_date

  • + 0 comments

    Try this using window functions and CTE:

    -- submissions_per_day
    with hacker_submissions as
    (select hacker_id, submission_date,count(submission_id) submission_cnt
        from submissions group by hacker_id, submission_date),
    
    -- rank hackers on submission_cnt
    ranked_hackers as (select hs.submission_date,hs.hacker_id, submission_cnt,
        row_number() over (partition by hs.submission_date order by submission_cnt desc, hs.hacker_id asc) rnk
        from hacker_submissions hs),
    
    -- eligible hackers
    cte as (select hacker_id, submission_date,
    count(submission_cnt) over (partition by hacker_id order by submission_date) date_before_submissions
    from hacker_submissions),
    eligible_hackers as (select hacker_id,submission_date from cte where
                        day(submission_date) = date_before_submissions),
    
    -- unique eligible hackers count
    unique_hackers as (select submission_date, count(hacker_id) unique_cnt
                       from eligible_hackers group by submission_date)
    
    --combine tables
    select rh.submission_date,uh.unique_cnt,rh.hacker_id,h.name from
    ranked_hackers rh
    inner join unique_hackers uh on uh.submission_date=rh.submission_date
    inner join hackers h on h.hacker_id = rh.hacker_id
    where rh.rnk=1
    order by rh.submission_date
    
  • + 0 comments

    If you're having trouble with CTE

    Running:

    SELECT Version();

    Returns: 5.7.27-0ubuntu0.18.04.1

    which, according to my mentor (chatgpt), indicates that the version of MySQL running in the backend of this particular problem is not version 8.0 or later, so will not support CTEs

  • + 0 comments
    SELECT A.submission_date, A.unique_hackers, B.top_hacker_id, B.name
    FROM
    
    (SELECT submission_date AS submission_date, SUM(unique_hackers) AS unique_hackers FROM
        (SELECT s1.submission_date AS submission_date, COUNT(DISTINCT s2.hacker_id) AS unique_hackers
        FROM Submissions s1 JOIN Submissions s2
        ON s2.submission_date <= s1.submission_date
        GROUP BY s1.submission_date, s2.hacker_id
        HAVING COUNT(DISTINCT s2.submission_date) = DAY(s1.submission_date)
        ORDER BY s1.submission_date) uk
    GROUP BY submission_date 
    ORDER BY submission_date) A
    
    JOIN
    
    (SELECT t.submission_date AS submission_date, t.top_hacker_id AS top_hacker_id, h.name AS name
    FROM
    (SELECT s3.submission_date AS submission_date, 
        (SELECT hacker_id FROM Submissions s4
         WHERE s4.submission_date = s3.submission_date
         GROUP BY s4.hacker_id
         ORDER BY COUNT(s4.submission_id) DESC, s4.hacker_id 
         LIMIT 1) AS top_hacker_id
     FROM Submissions s3
     GROUP BY s3.submission_date) t 
    JOIN Hackers h
    ON t.top_hacker_id = h.hacker_id
    ORDER BY t.submission_date) B
    
    ON A.submission_date = B.submission_date
    ORDER BY A.submission_date;
    
  • + 0 comments
    Check this Out Guys  
    
    SELECT A.SUBMISSION_DATE , A.CNT , B.CL2 , B.NAME FROM 
    (
        SELECT TB8.SUBMISSION_DATE , COUNT(TB8.HACKER_ID) AS CNT FROM 
    (
        SELECT TB6.SUBMISSION_DATE , TB6.HACKER_ID FROM 
        (
            SELECT SUBMISSION_DATE , HACKER_ID 
            FROM SUBMISSIONS GROUP BY SUBMISSION_DATE , HACKER_ID
        )AS TB6
        WHERE TB6.HACKER_ID IN 
        (
            SELECT TB7.HACKER_ID FROM 
            (
                SELECT SUBMISSION_DATE , HACKER_ID
                FROM SUBMISSIONS GROUP BY SUBMISSION_DATE , HACKER_ID
            ) AS TB7
            WHERE TB7.SUBMISSION_DATE <= TB6.SUBMISSION_DATE
            GROUP BY TB7.HACKER_ID 
            HAVING COUNT(TB7.HACKER_ID) = DATEDIFF(TB6.SUBMISSION_DATE,'2016-03-01')+1  
        )
    ) AS TB8
    GROUP BY TB8.SUBMISSION_DATE
    ) AS A 
    JOIN 
    (
    SELECT A.SUBMISSION_DATE , A.CL2 , B.NAME FROM 
    (
        SELECT TB2.SUBMISSION_DATE ,
        (
            SELECT HACKER_ID
            FROM SUBMISSIONS 
            WHERE SUBMISSION_DATE = TB2.SUBMISSION_DATE
            GROUP BY SUBMISSION_DATE, HACKER_ID
            ORDER BY COUNT(HACKER_ID) DESC , HACKER_ID ASC LIMIT 1 
        ) AS CL2
        FROM 
        (SELECT DISTINCT (SUBMISSION_DATE) AS SUBMISSION_DATE FROM SUBMISSIONS)
        AS TB2
    ) AS A
    LEFT JOIN 
    HACKERS AS B 
    ON A.CL2 = B.HACKER_ID
    ) AS B 
    ON A.SUBMISSION_DATE = B.SUBMISSION_DATE 
    ORDER BY A.SUBMISSION_DATE