牛客SQL 大厂面试真题 某度信息流 6套代码及解析

2.用户增长场景(某度信息流)

SQL162 2021年11月每天的人均浏览文章时长

select 
date_format(in_time,'%Y-%m-%d') as dt,
round(sum(timestampdiff(second,in_time,out_time))/count(distinct uid),1) as avg_viiew_len_sec
from
tb_user_log
where date_format(in_time,'%Y-%m')='2021-11' and artical_id !=0
group by dt
order by avg_viiew_len_sec

SQL163 每篇文章同一时刻最大在看人数
( 1.’ 1 diff ’ 表示的意思为增加列名为diff 的临时列,值全为1 ;
2. 题目要求在瞬时统计时遵循【先进后出】:如果同一时刻有进入也有离开时,先记录用户数增加,再记录减少。
因此在ORDER BY层面,在遵循dt升序的同时,还要遵循先+1,再-1的原则,即diff DESC:
SUM(diff) OVER(PARTITION BY artical_id ORDER BY dt, diff DESC) )

select 
artical_id,
max(instant_uv) as max_uv
from(
    select artical_id,
    sum(diff)over(partition by artical_id order by dt,diff desc) as instant_uv
from (
    select artical_id,in_time as dt, 1 diff
    from tb_user_log
    where artical_id !=0
    union all
    select artical_id,out_time as dt, -1 diff
    from tb_user_log
    where artical_id !=0 ) t1
) t2
group by artical_id
order by max_uv desc

SQL164 2021年11月每天新用户的次日留存率
(1.DATE_SUB() 函数指的是从日期减去指定的时间间隔,
interval为取间隔的含义,在这个SQL中,子查询的where条件中含义说白了就是:
当A表时间 = B表时间 - 1天
2.倒数第四行a.dt=date_sub(b.dt,interval 1 day)前面为什么要用and连接,而不用where呢?
on 表示的是两个表连接的依据(本题中表连接的依据是UID相等且日期相差一天),
where表示的是进行筛选,
在本题目中,通过where的子句进行筛选,第二天未活跃的数据都过滤掉了。
)

select a.dt,
round(count(b.uid)/count(a.uid),2) as uv_left_rate
from(
    select uid,
    min(date(in_time)) as dt
    from tb_user_log
    group by uid
    ) as a
left join
    (
    select uid,
    date(in_time) as dt
    from tb_user_log
    union
    select uid,
    date(out_time) as dt
    from tb_user_log    
    ) as b
on a.uid=b.uid
and a.dt=date_sub(b.dt,interval 1 day)
where date_format(a.dt,'%Y%m')='202111'
group by a.dt
order by a.dt

SQL165 统计活跃间隔对用户分级结果

select user_grade,
round(count(uid)/(select count(distinct uid) from tb_user_log ),2)  as ratio
from (
    select uid,
    (case when datediff((select max(in_time) from tb_user_log) , max(in_time))<=6
    and datediff((select max(in_time) from tb_user_log) , min(in_time))>6 then '忠实用户'
    when datediff((select max(in_time) from tb_user_log),max(in_time))<=6
    and datediff((select max(in_time) from tb_user_log) , min(in_time))<=6 then '新晋用户'
    when  datediff((select max(in_time) from tb_user_log),max(in_time))>=7
    and  datediff((select max(in_time) from tb_user_log) , min(in_time))<=29 then '沉睡用户'
    else '流失用户' end ) as user_grade
    from  tb_user_log
    group by uid
    ) as a
group by user_grade
order by ratio desc

SQL166 每天的日活数及新用户占比

select dt,
count(*) as dau,
round(sum(new)/count(*),2)
from(
    select dt,uid,
    case when dt=first then 1 else 0 end as new
    from (
        select uid,
        date(in_time) as dt
        from tb_user_log
        union 
        select uid,
        date(out_time) as dt
        from tb_user_log
        ) as a
    left join
        (
        select uid,
        min(date(in_time)) as first
        from tb_user_log
        group by uid
        ) as b
        using(uid)
) as c
group by dt
order by dt

SQL167 连续签到领金币
(思路如下:
在这里插入图片描述
1.%7 是什么意思
% 号的意思是取余,%7表示除以7的余数,3除以7 等于 0 余3, 8除以7等于1 余 1;
2. 对上图的补充解释:
本题的难点在于怎么求得连续性,通过a表中的row_number排序,给每个dt一个对应的序号;
然后通过b表中的subdate求得签到日期dt与row_number得出的序列号的差值,如果差值不变,即签到日期与序列同步变化,提升一天,序号增加1,则表示他在连续签到; 提升多天,序号增加1,差值不恒定,则表示他没有连续签到;
通过c表中的row_number给差值分组进行排序,差值不变,即连续签到,则单独分组进行排序;差值变动,则表示没有连续签到,另外分一组再次进行排序。对结果用%取余数;
最后用case when 分情况讨论,余数为三时,加三个积分。余数为0时,表示连续了七天,加七个积分。)

select uid,date_format(dt,'%Y%m') as month
,sum(case when stage_index=3 then 3
   when stage_index=0 then 7
   else 1 end) as coin
from(
    select uid,dt
    ,(row_number()over(partition by uid,init_date order by dt))%7 as stage_index
    from
    (
        select uid,dt,rn,subdate(dt,rn) as init_date
        from (
            select uid,date(in_time) as dt
            ,row_number()over(partition by uid order by date(in_time)) as rn
            from tb_user_log
            where date(in_time) >='2021-07-07'
            and date(in_time)<'2021-11-01'
            and artical_id=0 and sign_in=1
        ) as a
    ) as b
) as c
group by uid,month
order by month,uid;

猜你喜欢

转载自blog.csdn.net/qq118640594X/article/details/127378234