题解 | #牛客每个人最近的登录日期(五)#

牛客每个人最近的登录日期(五)

https://www.nowcoder.com/practice/ea0c56cd700344b590182aad03cc61b8

select
t1.date,
round(if(t2.first_day is null, 0 , t2.next_act_per ),3) p
from(
    select date
    from login
    group by date
) t1
left join(
    select
    first_day,
    if (
        count(
            case
                when date_diff = 0 then user_id
            end
        ) = 0,
        0,
        count(
            case
                when date_diff = 1 then user_id
            end
        ) / count(
            case
                when date_diff = 0 then user_id
            end
        )
    ) as next_act_per
from
    (
        select
            user_id,
            date,
            min(date) over (
                partition by
                    user_id
                order by
                    date asc
            ) as first_day,
            datediff(
                date,
                min(date) over (
                    partition by
                        user_id
                    order by
                        date asc
                )
            ) as date_diff
        from
            login
    ) t
group by
    first_day
order by
    first_day asc

) t2 on t1.date = t2.first_day

利用窗口函数

全部评论

相关推荐

04-08 23:37
已编辑
东华大学 结构工程师
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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