博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

sql-常见面试题汇总

Posted on 2021-08-01 18:17  隐灰子  阅读(59)  评论(0编辑  收藏  举报

1.连续7天登陆但未下单的用户

select c.user_id,
count(c.cum) as cum_01 
from 
(select  a.user_id,a.time01 - row_number() over(partition by a.user_id order_by a.time01 asc) as cum from 
    (
        select distinct user_id ,to_char(event_time,'yyyymmdd') as time01
        from user_behaviour
        where event_name='login' ) a 
    left join 
    (select distinct user_id ,to_char(order_time,'yyyymmdd') as time02
        from order) b 
    on a.user_id=b.user_id
    and a.time01=b.time02
    where b.time02 is null) c 
group by c.user_id
having count(c.cum) >=7;