with
cte1 as (
select
loc_nm,
coalesce(
case loc_nm
when '北京机床研究所' then '写字楼'
when '将台西' then '地铁站'
else loc_type
end,
loc_type
) as loc_type
from gd_loc_map
),
cte2 as (
select
t1.user_id,
date_format(t1.start_time, '%Y-%m-01') as YM,
count(distinct date_format(t1.start_time, '%Y-%m-%d')) as date_cnt
from hello_bike_riding_rcd t1
join cte1 t2 on t1.start_loc = t2.loc_nm
join cte1 t3 on t1.end_loc = t3.loc_nm
where year(t1.start_time) between 2020 and 2024
and t2.loc_type <> t3.loc_type
and t2.loc_type in ('写字楼', '地铁站')
and t3.loc_type in ('写字楼', '地铁站')
group by 1,2
),
cte3 as (
select
user_id
from (
select
user_id,
date_sub(YM, interval row_number() over (partition by user_id order by YM) month) as YM1
from (select user_id,YM from cte2 where date_cnt>=5) a
) grouped_data
group by user_id, YM1
having count(1) >= 3
)
select
a.user_id,
case when b.user_id is not null then 1 else 0 end as active_tag
from (
select distinct user_id from hello_bike_riding_rcd
) a
left join cte3 b on a.user_id = b.user_id;
with cte1 as
(select distinct usr_id
,date_format(login_time,'%Y-%m-%d') login_date
,min(date_format(login_time,'%Y-%m-%d')) over(partition by usr_id) first_date
from user_login_log),
cte2 as
(select distinct t1.usr_id
,t1.login_date
,t1.first_date
,max(coalesce(t2.login_date,t1.login_date)) last_date
from cte1 t1
left join cte1 t2
on t1.usr_id=t2.usr_id and t2.login_date<t1.login_date
group by 1,2,3),
cte3 as
(select usr_id
,date_sub(login_date,interval 2 day) login_date
,case when login_date=first_date then '新增用户'
when datediff(login_date,last_date) between 1 and 3 then '留存用户'
else '回流用户' end as usr_type
from cte2
where year(date_sub(login_date,interval 2 day))=2024)
select login_date
,concat(round(count(distinct (case when usr_type='新增用户' then usr_id end))/count(distinct usr_id)*100,2),', ',round(count(distinct (case when usr_type='留存用户' then usr_id end))/count(distinct usr_id)*100,2),', ',round(count(distinct (case when usr_type='回流用户' then usr_id end))/count(distinct usr_id)*100,2)) pct
from cte3
group by login_date
order by login_date
with cte1 as
(select distinct usr_id
,date_format(login_time,'%Y-%m-%d') login_date
,min(date_format(login_time,'%Y-%m-%d')) over(partition by usr_id) first_date
from user_login_log),
cte2 as
(select distinct t1.usr_id
,t1.login_date
,t1.first_date
,max(coalesce(t2.login_date,t1.login_date)) last_date
from cte1 t1
left join cte1 t2
on t1.usr_id=t2.usr_id and t2.login_date<t1.login_date
group by 1,2,3),
cte3 as
(select usr_id
,login_date
,case when login_date=first_date then '新增用户'
when datediff(login_date,last_date) between 1 and 3 then '留存用户'
else '回流用户' end as usr_type
from cte2
where year(login_date)=2024)
select login_date
,concat(round(count(distinct (case when usr_type='新增用户' then usr_id end))/count(distinct usr_id)*100,2),', ',round(count(distinct (case when usr_type='留存用户' then usr_id end))/count(distinct usr_id)*100,2),', ',round(count(distinct (case when usr_type='回流用户' then usr_id end))/count(distinct usr_id)*100,2)) pct
from cte3
group by login_date
order by login_date
with
cte1 as (
select
loc_nm,
coalesce(
case loc_nm
when '北京机床研究所' then '写字楼'
when '将台西' then '地铁站'
else loc_type
end,
loc_type
) as loc_type
from gd_loc_map
),
cte2 as (
select
distinct user_id,
date_format(start_time, '%Y-%m-01') as YM,
date_format(start_time, '%Y-%m-%d') as start_date
from hello_bike_riding_rcd t1
join cte1 t2 on t1.start_loc = t2.loc_nm
join cte1 t3 on t1.end_loc = t3.loc_nm
where year(start_time) between 2020 and 2024
and t2.loc_type <> t3.loc_type
and t2.loc_type in ('写字楼', '地铁站')
and t3.loc_type in ('写字楼', '地铁站')
),
cte3 as (
select
user_id,
YM,
count(distinct start_date) as cnt
from cte2
group by user_id, YM
having cnt >= 5
),
cte4 as (
select
user_id
from (
select
user_id,
date_sub(YM, interval row_number() over (partition by user_id order by YM) month) as YM1
from cte3
) subquery
group by user_id, YM1
having count(user_id) >= 3
)
select
a.user_id,
case when b.user_id is not null then 1 else 0 end as active_tag
from (
select distinct user_id from hello_bike_riding_rcd
) a
left join cte4 b on a.user_id = b.user_id;
with cte1 as(
select loc_nm
,case when loc_nm='北京机床研究所' then '写字楼'
when loc_nm='将台西' then '地铁站'
else loc_type
end as loc_type
from gd_loc_map
),
cte2 as(
select distinct user_id
,concat(date_format(start_time,"%Y-%m"),'-01') YM
,date_format(start_time,"%Y-%m-%d") start_date
from hello_bike_riding_rcd t1
left join cte1 t2 on t1.start_loc=t2.loc_nm
left join cte1 t3 on t1.end_loc=t3.loc_nm
where year(start_time) between 2020 and 2024
and t2.loc_type in ('写字楼','地铁站')
and t3.loc_type in ('写字楼','地铁站')
and t2.loc_type<>t3.loc_type
),
cte3 as(
select user_id
,YM
,count(distinct start_date) cnt
from cte2 a
group by 1,2
having count(distinct start_date)>=5),
cte4 as
(select distinct user_id
from
(select user_id
,date_sub(YM,interval row_number() over(partition by user_id order by YM) month) YM1
from cte3 a) b
group by user_id,YM1
having count(user_id)>=3
)
select distinct a.user_id
,case when b.user_id is null then 0 else 1 end as active_tag
from (select distinct user_id from hello_bike_riding_rcd) a left join cte4 b
on a.user_id=b.user_id
with cte1 as(
select loc_nm
,case
when loc_nm='凯德广场' then '购物广场'
when loc_nm='将台西' then '地铁站'
else loc_type
end as loc_type
from gd_loc_map
),
cte2 as(
select distinct user_id
,concat(date_format(start_time,"%Y-%m"),'-01') YM
,date_format(start_time,"%Y-%m-%d") start_date
from hello_bike_riding_rcd t1
left join cte1 t2 on t1.start_loc=t2.loc_nm
left join cte1 t3 on t1.end_loc=t3.loc_nm
where year(start_time) between 2020 and 2024
and t2.loc_type in ('写字楼','地铁站')
and t3.loc_type in ('写字楼','地铁站')
and t2.loc_type<>t3.loc_type
),
cte3 as(
select user_id
,YM
,count(distinct start_date) cnt
from cte2 a
group by 1,2
having count(distinct start_date)>=5),
cte4 as
(select distinct user_id
from
(select user_id
,date_sub(YM,interval row_number() over(partition by user_id order by YM) month) YM1
from cte3 a) b
group by user_id,YM1
having count(user_id)>=3
)
select distinct a.user_id
,case when b.user_id is null then 0 else 1 end as active_tag
from (select distinct user_id from hello_bike_riding_rcd) a left join cte4 b
on a.user_id=b.user_id
with cte1 as(
select loc_nm
,case when loc_nm='北京机床研究所' then '写字楼'
when loc_nm='凯德广场' then '购物广场'
when loc_nm='将台西' then '地铁站'
else loc_type
end as loc_type
from gd_loc_map
),
cte2 as(
select distinct user_id
,concat(date_format(start_time,"%Y-%m"),'-01') YM
,date_format(start_time,"%Y-%m-%d") start_date
from hello_bike_riding_rcd t1
left join cte1 t2 on t1.start_loc=t2.loc_nm
left join cte1 t3 on t1.end_loc=t3.loc_nm
where year(start_time) between 2020 and 2024
and t2.loc_type in ('写字楼','地铁站')
and t3.loc_type in ('写字楼','地铁站')
and t2.loc_type<>t3.loc_type
),
cte3 as(
select user_id
,YM
,count(distinct start_date) cnt
from cte2 a
group by 1,2
having count(distinct start_date)>=5),
cte4 as
(select distinct user_id
from
(select user_id
,YM
,date_sub(YM,interval row_number() over(partition by user_id order by YM) month) YM1
from cte3 a) b
group by user_id,YM1
having count(distinct YM)>=3
)
select distinct user_id
, 1 as active_tag
from cte4
with cte1 as(
select loc_nm
,case when loc_nm='北京机床研究所' then '写字楼'
when loc_nm='凯德广场' then '购物广场'
when loc_nm='将台西' then '地铁站'
else loc_type
end as loc_type
from gd_loc_map
),
cte2 as(
select distinct user_id
,concat(date_format(start_time,"%Y-%m"),'-01') YM
,date_format(start_time,"%Y-%m-%d") start_date
from hello_bike_riding_rcd t1
left join cte1 t2 on t1.start_loc=t2.loc_nm
left join cte1 t3 on t1.end_loc=t3.loc_nm
where year(start_time) between 2020 and 2024
and t2.loc_type in ('写字楼','地铁站')
and t3.loc_type in ('写字楼','地铁站')
and t2.loc_type<>t3.loc_type
),
cte3 as(
select user_id
,YM
,count(distinct start_date) cnt
from cte2 a
group by 1,2
having count(distinct start_date)>=5),
cte4 as
(select distinct user_id
from
(select user_id
,YM
,date_sub(YM,interval row_number() over(partition by user_id order by YM) month) YM1
from cte3 a) b
group by user_id,YM1
having count(distinct YM)>=3
)
select distinct a.user_id
,case when b.user_id is null then 0 else 1 end as active_tag
from (select distinct user_id from hello_bike_riding_rcd) a left join cte4 b
on a.user_id=b.user_id
order by user_id
with cte1 as(
select loc_nm
,case when loc_nm='北京机床研究所' then '写字楼'
when loc_nm='凯德广场' then '购物广场'
when loc_nm='将台西' then '地铁站'
else loc_type
end as loc_type
from gd_loc_map
),
cte2 as(
select distinct user_id
,concat(date_format(start_time,"%Y-%m"),'-01') YM
,date_format(start_time,"%Y-%m-%d") start_date
from hello_bike_riding_rcd t1
left join cte1 t2 on t1.start_loc=t2.loc_nm
left join cte1 t3 on t1.end_loc=t3.loc_nm
where year(start_time) between 2020 and 2024
and t2.loc_type in ('写字楼','地铁站')
and t3.loc_type in ('写字楼','地铁站')
and t2.loc_type<>t3.loc_type
),
cte3 as(
select user_id
,YM
,count(distinct start_date) cnt
from cte2 a
group by 1,2
having count(distinct start_date)>=5),
cte4 as
(select distinct user_id
from
(select user_id
,YM
,date_sub(YM,interval row_number() over(partition by user_id order by YM) month) YM1
from cte3 a) b
group by user_id,YM1
having count(distinct YM)>=3
)
select distinct a.user_id
,case when b.user_id is null then 0 else 1 end as active_tag
from (select distinct user_id from hello_bike_riding_rcd) a left join cte4 b
on a.user_id=b.user_id
with cte1 as(
select loc_nm
,case when loc_nm='北京机床研究所' then '写字楼'
when loc_nm='凯德广场' then '购物广场'
when loc_nm='将台西' then '地铁站'
else loc_type
end as loc_type
from gd_loc_map
),
cte2 as(
select distinct user_id
,concat(date_format(start_time,"%Y-%m"),'-01') YM
,date_format(start_time,"%Y-%m-%d") start_date
from hello_bike_riding_rcd t1
left join cte1 t2 on t1.start_loc=t2.loc_nm
left join cte1 t3 on t1.end_loc=t3.loc_nm
where year(start_time) between 2020 and 2024
and t2.loc_type in ('写字楼','地铁站')
and t3.loc_type in ('写字楼','地铁站')
and t2.loc_type<>t3.loc_type
),
cte3 as(
select user_id
,YM
,count(distinct start_date) cnt
from cte2 a
group by 1,2
having count(distinct start_date)>=5),
cte4 as
(select distinct user_id
from
(select user_id
,date_sub(YM,interval row_number() over(partition by user_id order by YM) month) YM1
from cte3 a) b
group by user_id,YM1
having count(user_id)>=3
)
select distinct a.user_id
,case when b.user_id is null then 0 else 1 end as active_tag
from (select distinct user_id from hello_bike_riding_rcd) a left join cte4 b
on a.user_id=b.user_id
with cte1 as(
select loc_nm
,case when loc_nm='北京机床研究所' then '写字楼'
when loc_nm='凯德广场' then '购物广场'
when loc_nm='将台西' then '地铁站'
else loc_type
end as loc_type
from gd_loc_map
),
cte2 as(
select distinct user_id
,date_format(start_time,"%Y%m") YM
,date_format(start_time,"%Y-%m-%d") start_date
from hello_bike_riding_rcd t1
left join cte1 t2 on t1.start_loc=t2.loc_nm
left join cte1 t3 on t1.end_loc=t3.loc_nm
where year(start_time) between 2020 and 2024
and t2.loc_type in ('写字楼','地铁站')
and t3.loc_type in ('写字楼','地铁站')
),
cte3 as(
select user_id
,YM
,count(distinct start_date) cnt
from cte2 a
group by 1,2
having count(distinct start_date)>=5),
cte4 as
(select distinct user_id
from
(select user_id
,YM-row_number() over(partition by user_id order by YM) YM1
from cte3 a) b
group by user_id,YM1
having count(user_id)>=3
)
select distinct a.user_id
,case when b.user_id is null then 0 else 1 end as active_tag
from (select distinct user_id from hello_bike_riding_rcd) a left join cte4 b
on a.user_id=b.user_id
with cte1 as(
select loc_nm
,case when loc_nm='北京机床研究所' then '写字楼'
when loc_nm='凯德广场' then '购物广场'
when loc_nm='将台西' then '地铁站'
else loc_type
end as loc_type
from gd_loc_map
),
cte2 as(
select distinct user_id
,date_format(start_time,"%Y%m") YM
,date_format(start_time,"%Y-%m-%d") start_date
from hello_bike_riding_rcd t1
left join cte1 t2 on t1.start_loc=t2.loc_nm
left join cte1 t3 on t1.end_loc=t3.loc_nm
where year(start_time) between 2020 and 2024
and t2.loc_type in ('写字楼','地铁站')
and t3.loc_type in ('写字楼','地铁站')
and t2.loc_type<>t3.loc_type
),
cte3 as(
select user_id
,YM
,count(distinct start_date) cnt
from cte2 a
group by 1,2
having count(distinct start_date)>=5),
cte4 as
(select distinct user_id
from
(select *
,YM-row_number() over(partition by user_id order by YM) YM1
from cte3 a) b
group by user_id,YM1
having count(user_id)>=3
)
select distinct a.user_id
,case when b.user_id is null then 0 else 1 end as active_tag
from (select distinct user_id from hello_bike_riding_rcd) a left join cte4 b
on a.user_id=b.user_id
with cte1 as(
select loc_nm
,case when loc_nm='北京机床研究所' then '写字楼'
when loc_nm='凯德广场' then '购物广场'
when loc_nm='将台西' then '地铁站'
else loc_type
end as loc_type
from gd_loc_map
),
cte2 as(
select distinct user_id
,date_format(start_time,"%Y-%m-%d") start_date
from hello_bike_riding_rcd t1
left join cte1 t2 on t1.start_loc=t2.loc_nm
left join cte1 t3 on t1.end_loc=t3.loc_nm
where year(start_time) between 2020 and 2024
and t2.loc_type in ('写字楼','地铁站')
and t3.loc_type in ('写字楼','地铁站')
and t2.loc_type<>t3.loc_type
),
cte3 as(
select user_id
,date_format(start_date,"%Y%m") YM
,count(distinct start_date) cnt
from cte2 a
group by 1,2),
cte4 as
(select distinct user_id
from
(select *
,row_number() over(partition by user_id order by YM) rnk
,YM-row_number() over(partition by user_id order by YM) YM1
from (select * from cte3 where cnt>=5) a) b
group by user_id,YM1
having count(user_id)>=3
)
select distinct a.user_id
,case when b.user_id is null then 0 else 1 end as active_tag
from (select distinct user_id from hello_bike_riding_rcd) a left join cte4 b
on a.user_id=b.user_id
with cte1 as(
select loc_nm
,case when loc_nm='北京机床研究所' then '写字楼'
when loc_nm='凯德广场' then '购物广场'
when loc_nm='将台西' then '地铁站'
else loc_type
end as loc_type
from gd_loc_map
),
cte2 as(
select t1.*
,t2.loc_type start_loc_type
,t3.loc_type end_loc_type
from hello_bike_riding_rcd t1
left join cte1 t2 on t1.start_loc=t2.loc_nm
left join cte1 t3 on t1.end_loc=t3.loc_nm
where year(start_time) between 2020 and 2024
and t2.loc_type in ('写字楼','地铁站')
and t3.loc_type in ('写字楼','地铁站')
and t2.loc_type<>t3.loc_type
),
cte3 as(
select user_id
,date_format(start_date,"%Y%m") YM
,count(distinct start_date) cnt
from (select distinct user_id,date_format(start_time,"%Y-%m-%d") start_date from cte2) a
group by 1,2),
cte4 as
(select distinct user_id
from
(select *
,row_number() over(partition by user_id order by YM) rnk
,YM-row_number() over(partition by user_id order by YM) YM1
from (select * from cte3 where cnt>=5) a) b
group by user_id,YM1
having count(user_id)>=3
)
select distinct a.user_id
,case when b.user_id is null then 0 else 1 end as active_tag
from (select distinct user_id from hello_bike_riding_rcd) a left join cte4 b
on a.user_id=b.user_id
with cte1 as(
select loc_nm
,case when loc_nm='北京机床研究所' then '写字楼'
when loc_nm='凯德广场' then '购物广场'
when loc_nm='将台西' then '地铁站'
else loc_type
end as loc_type
from gd_loc_map
),
cte2 as(
select t1.*
,t2.loc_type start_loc_type
,t3.loc_type end_loc_type
from hello_bike_riding_rcd t1
left join cte1 t2 on t1.start_loc=t2.loc_nm
left join cte1 t3 on t1.end_loc=t3.loc_nm
where year(start_time) between 2020 and 2024
and t2.loc_type in ('写字楼','地铁站')
and t3.loc_type in ('写字楼','地铁站')
and t2.loc_type<>t3.loc_type
),
cte3 as(
select distinct user_id
,date_format(start_time,"%Y%m") YM
,count(distinct date_format(start_time,"%Y-%m-%d")) cnt
from cte2 a
group by 1,2
having count(distinct date_format(start_time,"%Y-%m-%d"))>=5),
cte4 as
(select distinct user_id
from
(select *
,row_number() over(partition by user_id order by YM) rnk
,YM-row_number() over(partition by user_id order by YM) YM1
from cte3 a) b
group by user_id,YM1
having count(user_id)>=3
)
select distinct a.user_id
,case when b.user_id is null then 0 else 1 end as active_tag
from (select distinct user_id from hello_bike_riding_rcd) a left join cte4 b
on a.user_id=b.user_id
with cte1 as(
select loc_nm
,case when loc_nm='北京机床研究所' then '写字楼'
when loc_nm='凯德广场' then '购物广场'
when loc_nm='将台西' then '地铁站'
else loc_type
end as loc_type
from gd_loc_map
),
cte2 as(
select t1.*
,t2.loc_type start_loc_type
,t3.loc_type end_loc_type
from hello_bike_riding_rcd t1
left join cte1 t2 on t1.start_loc=t2.loc_nm
left join cte1 t3 on t1.end_loc=t3.loc_nm
where year(start_time) between 2020 and 2024
and t2.loc_type in ('写字楼','地铁站')
and t3.loc_type in ('写字楼','地铁站')
and t2.loc_type<>t3.loc_type
),
cte3 as(
select user_id
,date_format(start_time,"%Y%m") YM
,count(distinct date_format(start_time,"%Y-%m-%d")) cnt
from cte2 a
group by 1,2
having count(distinct date_format(start_time,"%Y-%m-%d"))>=5),
cte4 as
(select distinct user_id
from
(select *
,row_number() over(partition by user_id order by YM) rnk
,YM-row_number() over(partition by user_id order by YM) YM1
from cte3 a) b
group by user_id,YM1
having count(user_id)>=3
)
select distinct a.user_id
,case when b.user_id is null then 0 else 1 end as active_tag
from (select distinct user_id from hello_bike_riding_rcd) a left join cte4 b
on a.user_id=b.user_id
with cte1 as(
select loc_nm
,case when loc_nm='北京机床研究所' then '写字楼'
when loc_nm='凯德广场' then '购物广场'
when loc_nm='将台西' then '地铁站'
else loc_type
end as loc_type
from gd_loc_map
),
cte2 as(
select t1.*
,t2.loc_type start_loc_type
,t3.loc_type end_loc_type
from hello_bike_riding_rcd t1
left join cte1 t2 on t1.start_loc=t2.loc_nm
left join cte1 t3 on t1.end_loc=t3.loc_nm
where year(start_time) between 2020 and 2024
and t2.loc_type in ('写字楼','地铁站')
and t3.loc_type in ('写字楼','地铁站')
and t2.loc_type<>t3.loc_type
),
cte3 as(
select user_id
,date_format(start_date,"%Y%m") YM
,count(distinct start_date) cnt
from (select distinct user_id,date_format(start_time,"%Y-%m-%d") start_date from cte2) a
group by 1,2
having count(distinct start_date)>=5),
cte4 as
(select distinct user_id
from
(select *
,row_number() over(partition by user_id order by YM) rnk
,YM-row_number() over(partition by user_id order by YM) YM1
from cte3 a) b
group by user_id,YM1
having count(user_id)>=3
)
select distinct a.user_id
,case when b.user_id is null then 0 else 1 end as active_tag
from (select distinct user_id from hello_bike_riding_rcd) a left join cte4 b
on a.user_id=b.user_id