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

截至当月的练题情况

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

全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

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