题解 | 查询连续入住多晚的客户信息?

查询连续入住多晚的客户信息?

https://www.nowcoder.com/practice/5b4018c47dfd401d87a5afb5ebf35dfd

with
t1 as(
    select
        user_id,
        ct.room_id as room_id,
        gt.room_type as room_type,
        checkin_time,
        checkout_time,
        timestampdiff(day,checkin_time,checkout_time) +
            case
                when timestampdiff(second,checkin_time,checkout_time)%86400>0 then 1
                else 0
            end as days
    from
        checkin_tb as ct left join guestroom_tb as gt using(room_id)
)
,t2 as(
    select
        user_id,
        room_id,
        room_type,
        days,
        rank()over(partition by room_id order by days desc) as drank
    from
        t1
    where
        days>=2
)
,t3 as(
    select
        user_id,
        room_id,
        room_type,
        days
    from
        t2
    where
        drank=1
    order by
        days,
        room_id,
        user_id desc
)


select * from t3

全部评论

相关推荐

明天不下雨了:这个项目 这个简历 这个模板 莫不是一个开源的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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