题解 | #牛客每个人最近的登录日期(五)#
牛客每个人最近的登录日期(五)
https://www.nowcoder.com/practice/ea0c56cd700344b590182aad03cc61b8
select
t.all_date as date,
ifnull(round(t.c2 / t.c1,3),0) as p
from
(
select
t0.all_date,
count(distinct t1.user_id) c1,
count(distinct t2.user_id) c2
from
(
select
distinct date all_date
from
login
) t0
left join
(
select
user_id,
min(date) first_date
from
login
group by
user_id
) t1
on
t0.all_date = t1.first_date
left join
login t2
on
t2.user_id = t1.user_id and
t2.date = date_add(t1.first_date,interval 1 day)
group by
t0.all_date
) t
order by
t.all_date
t.all_date as date,
ifnull(round(t.c2 / t.c1,3),0) as p
from
(
select
t0.all_date,
count(distinct t1.user_id) c1,
count(distinct t2.user_id) c2
from
(
select
distinct date all_date
from
login
) t0
left join
(
select
user_id,
min(date) first_date
from
login
group by
user_id
) t1
on
t0.all_date = t1.first_date
left join
login t2
on
t2.user_id = t1.user_id and
t2.date = date_add(t1.first_date,interval 1 day)
group by
t0.all_date
) t
order by
t.all_date
