题解 | #最近7天每天的人均停留时长和次均有效时长#

最近7天每天的人均停留时长和次均有效时长

https://www.nowcoder.com/practice/7c0a23de67ad433e9fe6389afabd3496

考察内容:聚合函数、条件函数、日期函数、空值转换函数、排序

思路:

先计算每个用户每天的停留时长:timestampdiff(second, start_time, end_time)

② 计算最近7天每天的人均停留时长和次均有效时长:人均停留时长指每个人的总停留时长的平均值,次均有效时长指每次有效停留(假设停留时长小于3秒为无效)的时长平均值,可以用 if 或 case when 筛选有效停留的记录,用 round 保留1位小数。注意:如果当天没有有效的停留,此时数据位 null,需要用空值转换函数转为0,在 MySQL 中可以用 ifnull 函数,在 Hive 中可以用 nvl 函数

  • 人均停留时长 = 总停留时长/总人数:sum(stay_time_len) / count(distinct uid)
  • 次均有效时长 = 总有效停留时长/总有效停留次数:ifnull(sum(case when stay_time_len >= 3 then stay_time_len else 0 end) / count(case when stay_time_len >= 3 then stay_time_len end), 0)

③ 按照日期 dt 升序排序

select 
	dt,
	round(sum(stay_time_len) / count(distinct uid), 1) as avg_stay_time_len_user,
	round(ifnull(sum(case when stay_time_len >= 3 then stay_time_len else 0 end) / count(case when stay_time_len >= 3 then stay_time_len end), 0), 1) as avg_stay_time_len_once
from
(
	select
		uid,
		date(end_time) as dt,
		timestampdiff(second, start_time, end_time) as stay_time_len
	from
		user_play_log_tb
) a
where
  datediff((select max(date(end_time)) from user_play_log_tb), dt) < 7
group by dt
order by dt
;
全部评论

相关推荐

暴杀流调参工作者:春招又试了一些岗位,现在投递很有意思,不仅要精心准备简历,投递官网还得把自己写的东西一条一条复制上去,阿里更是各个bu都有自己的官网,重复操作无数次,投完简历卡完学历了,又该写性格测评、能力测评,写完了又要写专业笔试,最近还有些公司搞了AI辅助编程笔试,有些还有AI面试,对着机器人话也听不明白录屏硬说,终于到了人工面试又要一二三四面,小组成员面主管面部门主管面hr面,次次都没出错机会,稍有不慎就是挂。 卡学历卡项目卡论文卡实习什么都卡,没有不卡的😂
点赞 评论 收藏
分享
评论
2
1
分享

创作者周榜

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