题解 | #每份试卷每月作答数和截止当月的作答总数。#

每份试卷每月作答数和截止当月的作答总数。

https://www.nowcoder.com/practice/5f1cbe74c682485aa73e4c2b30f04a62

select 
exam_id,
date_format(start_time,"%Y%m") as start_month,
count(start_time) as month_cnt,
sum(count(start_time)) over (partition by exam_id order by date_format(start_time,"%Y%m")) as cum_exam_cnt 
from 
exam_record  
group by exam_id,start_month

# 考核知识点:

# 聚合窗口函数的使用,sum() over (patition by 字段 order by 字段 )

# patition by 是分组依据 order by 是排序依据 ,sum(count(start_time))是总和统计start_time的次数和,其中容易出错的地方是over()里面的排序依据,不可以直接写 order by start_month 会报错,必须写成date_format(start_time,"%Y%m")

注:以上内容仅为个人笔记,方便自己回头查看,若有题友提出建议,欢迎,望大家多多交流,互相学习

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-04 18:02
好不容易拿到了字节Offer,鼠鼠做后端的,但家里人觉得可能被裁员不稳定,让鼠鼠去投国企,现在好纠结到底该咋选
文档传偷助手:该投就投吧,不过建议别放弃offer 拿到手里的才是最好的
投递字节跳动等公司9个岗位
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
昨天 12:05
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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