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

月总刷题数和日均刷题数

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

select
    date_format (submit_time, '%Y%m') as submit_month,
    count(question_id) as month_q_cnt,
    (
        round(
            count(question_id) / max(day (last_day (submit_time))),
            3
        )
    ) as avg_day_q_cnt
from
    practice_record
where date_format(submit_time, '%Y') = '2021'
group by
    date_format (submit_time, '%Y%m')
union
select
    concat (submit_month, '汇总') as submit_month,
    month_q_cnt,
    avg_day_q_cnt
from
    (
        select
            date_format (submit_time, '%Y') as submit_month,
            count(question_id) as month_q_cnt,
            (
                round(
                    count(question_id) / max(day (last_day (submit_time))),
                    3
                )
            ) as avg_day_q_cnt
        from
            practice_record
        where date_format(submit_time, '%Y') = '2021'
        group by
            date_format (submit_time, '%Y')
    ) as temp
order by submit_month

因为涉及到聚合函数,所以在实现汇总的时候,使用了子查询先把年度汇总的数据整合算好,然后再使用concat拼接一下

全部评论

相关推荐

龙珠传说:nb,公务员解约不需要支付违约金吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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