题解 | #平均活跃天数和月活人数#

思路

这道题的关键点:

  1. 日期格式函数date_format
  2. 格式函数 round
  3. 正确理解:
  • 平均活跃天数怎么算? 平均活跃天数= 活跃的总天数 / 活跃人数
  • 活跃人数怎么算?

所以关键点就是算出活跃的总天数活跃人数

步骤

  1. 首先将数据划分成2021年的月内数据:分组
  2. 分别计算:
  • 活跃人数count(distinct uid)
  • 活跃的总天数 count(distinct uid, date_format (submit_time, '%y%m%d')) 会将相同的 uid 和日期视为一个单独的项,然后进行记数。这就意味着它会返回所有用户的活跃天数之和,即活跃的总天数,而不包含重复的值。

题解

select
    date_format (submit_time, '%Y%m') as month,
    round(
        (
            count(distinct uid, date_format (submit_time, '%y%m%d'))
        ) / count(distinct uid),
        2
    ) as avg_active_days,
    count(distinct uid) as mau
from
    exam_record
where
    submit_time is not null
    and year (submit_time) = 2021
group by
    date_format (submit_time, '%Y%m')
全部评论

相关推荐

09-25 00:00
已编辑
电子科技大学 Java
球球与墩墩:这不是前端常考的对象扁平化吗,面试官像是前端出来的 const flattern = (obj) => { const res = {}; const dfs = (curr, path) => { if(typeof curr === 'object' && curr !== null) { const isArray = Array.isArray(curr); for(let key in curr) { const newPath = path ? isArray ? `${path}[${key}]` : `${path}.${key}` : key; dfs(curr[key], newPath); } } else { res[path] = curr } } dfs(obj); return res; }
查看3道真题和解析
点赞 评论 收藏
分享
10-20 11:11
辽宁大学 营销
点赞 评论 收藏
分享
评论
2
2
分享

创作者周榜

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