题解 | #未完成率较高的50%用户近三个月答卷情况#

未完成率较高的50%用户近三个月答卷情况

https://www.nowcoder.com/practice/3e598a2dcd854db8b1a3c48e5904fe1c

select
    uid,
    date_format (start_time, '%Y%m') as start_month,
    count(start_time) as total_cnt,
    count(score) as complete_cnt
from
    (
        select
            uid,
            start_time,
            score,
            DENSE_RANK() OVER (
                PARTITION BY
                    uid
                ORDER BY
                    date_format (start_time, '%Y%m') desc
            ) as rk
        from
            exam_record
    ) as recent_months
where
    rk <= 3
    and uid in (
        select
            uid
        from
            user_info as ui
        where
            level = 6
            or level = 7
    )
    and uid in (
        SELECT
            uid
        FROM
            (
                SELECT
                    er.uid,
                    COUNT(submit_time) / COUNT(start_time) AS incomplete_rate,
                    NTILE (2) OVER (
                        ORDER BY
                            COUNT(submit_time) / COUNT(start_time) asc
                    ) AS rate_rank
                FROM
                    exam_record AS er
                    LEFT JOIN examination_info AS ei ON er.exam_id = ei.exam_id
                WHERE
                    ei.tag = 'SQL'
                GROUP BY
                    er.uid
            ) AS RankedUsers
        WHERE
            rate_rank = 1
    )
group by
    uid,
    start_month
order by
    uid,
    start_month
	
	虽然完成了 但是知道写得有点啰嗦 不够简洁美丽

全部评论

相关推荐

橙伟斌:我可以帮投客开实习,我这边需要人,我也开学要跑路了
投递用友等公司10个岗位
点赞 评论 收藏
分享
07-28 00:10
已编辑
门头沟学院 算法工程师
码农索隆:这哥们库库在我帖子下评论
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
08-07 15:30
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务