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

月总刷题数和日均刷题数

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练习日常#
全部评论

相关推荐

10-17 17:14
门头沟学院 C++
牛客410039819号:北京地区大多是919和927,这两场挂太多人了
投递华为等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务