题解 | #截至当月的练题情况#

截至当月的练题情况

https://www.nowcoder.com/practice/80ed62a097564fad880b967678e460fc

select 
  device_id,
  ym,
#   cnt, # 每个用户每月的练题数
  sum(cnt) over(partition by device_id order by ym) as sum_cnt,
#   sum(cnt) over(partition by device_id order by ym rows between 2 preceding and current row) as total3_cnt, 
  # 最近三个练题月的练题总数,between 2 preceding and current row表示当前行的上2行~当前行的汇总
#   count(ym) over(partition by device_id order by ym rows between 2 preceding and current row) as ym_cnt,
  # 月份数
  round(avg(cnt) over(partition by device_id order by ym rows between 2 preceding and current row), 2) as avg3_cnt,
  sum(cnt) over(order by ym) as total_cnt
from (
    select 
      device_id,
      date_format(event_date, '%Y-%m') as ym,
      count(question_id) as cnt
    #   sum(count(question_id)) over(partition by device_id order by date_format(event_date, '%Y-%m'))
    from
      question_practice_detail
    group by 
      device_id, ym
) a 
order by 
  device_id asc, ym asc

全部评论

相关推荐

合适才能收到offe...:招聘上写这些态度傲慢的就别继续招呼了,你会发现hr和面试官挺神的,本来求职艰难就可能影响一些心态了,你去这种公司面试的话,整个心态会炸的。
点赞 评论 收藏
分享
哈哈哈,你是老六:百度去年裁员分评不好,赶紧弄点红包
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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