题解 | #月总刷题数和日均刷题数#

月总刷题数和日均刷题数

https://www.nowcoder.com/practice/f6b4770f453d4163acc419e3d19e6746

SELECT
    date_format (A.SUBMIT_TIME, '%Y%m') as submit_month,
    COUNT(1) as month_q_cnt,
    ROUND(COUNT(1) / DAY_M, 3) as avg_day_q_cnt
FROM
    (
        SELECT
            *,
            DAYOFMONTH (LAST_DAY (SUBMIT_TIME)) DAY_M
        FROM
            practice_record
    ) A
WHERE
    YEAR (A.SUBMIT_TIME) = '2021'
GROUP BY
    date_format (A.SUBMIT_TIME, '%Y%m'),
    DAY_M
UNION ALL
SELECT
    '2021汇总',
    sum(X.month_q_cnt),
    ROUND(sum(X.month_q_cnt) / 31, 3)
FROM
    (
        SELECT
            date_format (A.SUBMIT_TIME, '%Y%m') as submit_month,
            COUNT(1) as month_q_cnt,
            ROUND(COUNT(1) / DAY_M, 3) as avg_day_q_cnt
        FROM
            (
                SELECT
                    *,
                    DAYOFMONTH (LAST_DAY (SUBMIT_TIME)) DAY_M
                FROM
                    practice_record
            ) A
        WHERE
            YEAR (A.SUBMIT_TIME) = '2021'
        GROUP BY
            date_format (SUBMIT_TIME, '%Y%m'),
            DAY_M
    ) X
group by
    YEAR (X.submit_month)
ORDER BY submit_month ASC;

#sql练习日常#
全部评论

相关推荐

不愿透露姓名的神秘牛友
11-26 15:46
已编辑
字节国际 电商后端 24k-35k
点赞 评论 收藏
分享
10-05 23:02
东北大学 Java
我说句实话啊:那时候看三个月培训班视频,随便做个项目背点八股,都能说3 40w是侮辱价
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务