输出的是和输出示例是一样的呀,问题在哪?
select
year(dt) as Y
, format(avg(REGEXP_SUBSTR(if(city = 'beijing', tmp_h, null), '-?[0-9]+')), 2)as beijing
, format(avg(REGEXP_SUBSTR(if(city = 'shanghai', tmp_h, null), '-?[0-9]+')), 2)as shanghai
, format(avg(REGEXP_SUBSTR(if(city = 'shenzhen', tmp_h, null), '-?[0-9]+')), 2)as shenzhen
, format(avg(REGEXP_SUBSTR(if(city = 'guangzhou', tmp_h, null), '-?[0-9]+')), 2)as guangzhou
from weather_rcd_china
where
year(dt) between 2011 and 2022
and city in ('beijing','shanghai','shenzhen','guangzhou')
group by 1
order by 1
记录一下第一次算峰值,想了很久,不知道还有没有其他的简单方法
with data1 as(
select usr_id, live_id, enter_time as time1, 1 as flag1
from ks_live_t1
union all
select usr_id, live_id, leave_time as time1, -1 as flag1
from ks_live_t1
),
data2 as(
select
live_id
, sum(flag1) over(partition by live_id order by time1 ) as online_users
from data1
)
select t1.live_id,t2.live_nm, max(online_users) as max_online_users
from data2 t1 left join ks_live_t2 t2 on t1.live_id = t2.live_id
group by 1, 2
order by 3 desc
如果活跃的定义就是登录的话,那么只需要判断最后一次登录时间是否 <=2024-10-01 不就可以吗
select count(1) as inactive_user_count
from(
select usr_id, max(login_time)
from user_login_log
group by 1
having max(login_time) <= '2024-10-01'
) tmp
with tmp as(
select
usr_id,
date_format(login_time,'%Y-%m-01') dt,
date_format(min(login_time)over(partition by usr_id),'%Y-%m-01') reg_dt
from user_login_log
wherelogin_time >= '2024-01-01' AND login_time < '2025-01-01'
),
tmp2 as(
select reg_dt, count(distinct usr_id) total_user
from tmp
group by 1
),
rete as(
select
reg_dt,
sum(case when min_gap = 1 then 1 else 0 end) rete
from(
select
usr_id,
reg_dt,
min(timestampdiff(month, reg_dt, dt)) min_gap
from tmp
where timestampdiff(month, reg_dt, dt) <> 0
group by 1, 2
) t1
group by1
)
select
a.reg_dt current_month,
round(ifnull(b.rete / a.total_user *100, 0), 2) t_plus_1_month_retention_rate
from tmp2 a left join rete b on a.reg_dt = b.reg_dt
with tmp1 as(
select
usr_id,
date_format(min(login_time),'%Y-%m-01') reg_dt
from user_login_log
where login_time >= '2024-01-01' AND login_time < '2025-01-01'
group by 1
),
tmp2 as(
select
usr_id,
date_format(login_time,'%Y-%m-01') dt
from user_login_log
where login_time >= '2024-01-01' AND login_time < '2025-01-01'
group by 1,2
)
select
a.reg_dt current_month ,
round(count(b.dt)*100 / count(a.reg_dt),2) t_plus_1_month_retention_rate
from tmp1 a left join tmp2 b
on a.usr_id = b.usr_id
and date(a.reg_dt) = date_sub(b.dt,interval 1 month)
group by 1
order by 1
with tmp as(
select
usr_id,
date_format(login_time,'%Y-%m-01') dt,
date_format(min(login_time)over(partition by usr_id),'%Y-%m-01') reg_dt
from user_login_log
wherelogin_time >= '2024-01-01' AND login_time < '2025-01-01'
)
select
a.reg_dt current_month ,
round(count(distinct b.usr_id)*100 / count(distinct a.usr_id),2) t_plus_1_month_retention_rate
from tmp a left join tmp b
on a.usr_id = b.usr_id
and date(a.reg_dt) = date_sub(b.dt,interval 1 month)
group by 1
order by 1
with tmp as(
select
usr_id,
date_format(login_time,'%Y-%m-01') dt,
date_format(min(login_time)over(partition by usr_id),'%Y-%m-01') reg_dt
from user_login_log
wherelogin_time >= '2024-01-01' AND login_time < '2025-02-01'
)
select
a.reg_dt current_month ,
round(count(distinct b.usr_id)*100 / count(distinct a.usr_id),2) t_plus_1_month_retention_rate
from tmp a left join tmp b
on a.usr_id = b.usr_id
and date(a.reg_dt) = date_sub(b.dt,interval 1 month)
group by 1
order by 1
with tmp1 as(
select
usr_id,
date_format(min(login_time),'%Y-%m-01') reg_dt
from user_login_log
where login_time >= '2024-01-01' AND login_time < '2025-01-01'
group by 1
),
tmp2 as(
select
usr_id,
date_format(login_time,'%Y-%m-01') dt
from user_login_log
where login_time >= '2024-01-01' AND login_time < '2025-01-01'
group by 1,2
)
select
a.reg_dt current_month ,
round(count(b.dt)*100 / count(a.reg_dt),2) t_plus_1_month_retention_rate
from tmp1 a left join tmp2 b
on a.usr_id = b.usr_id
and DATE_FORMAT(DATE_ADD(a.reg_dt, INTERVAL 1 MONTH), '%Y-%m-01') = b.dt
group by 1
order by 1
with tmp as(
select
usr_id,
date_format(login_time,'%Y-%m-01') dt
from user_login_log
wherelogin_time >= '2024-01-01' AND login_time < '2025-02-01'
group by 1,2
)
select
a.dt current_month ,
round(count(b.dt)*100 / count(a.dt),2) t_plus_1_month_retention_rate
from tmp a left join tmp b on a.usr_id = b.usr_id and date(a.dt) = date_sub(b.dt,interval 1 month)
where a.dt < '2024-12-01'
group by 1
order by 1
with tmp1 as(
select usr_id,date(login_time) dt
from user_login_log
where datediff(current_date(), date(login_time)) <= 90
group by 1,2
)
select
a.dt first_login_date,
round(count(distinct case when datediff(b.dt,a.dt) BETWEEN 1 AND 3then a.usr_id end ) *100/ count(distinct a.usr_id) ,2) t_plus_3_retention_rate,
round(count(distinct case when datediff(b.dt,a.dt) BETWEEN 1 AND 7then a.usr_id end ) *100/ count(distinct a.usr_id) ,2) t_plus_7_retention_rate,
round(count(distinct case when datediff(b.dt,a.dt) BETWEEN 1 AND 14 then a.usr_id end ) *100 /count(distinct a.usr_id) ,2) t_plus_14_retention_rate
from tmp1 a
left join tmp1 b
on a.usr_id = b.usr_id
and datediff(b.dt,a.dt) BETWEEN 1 AND 14
group by 1
order by 1
with tmp as(
select usr_id, date(login_time) dt
from user_login_log
where datediff(current_date(), date(login_time)) <=30
group by 1, 2
)
select
a.dt login_date,
concat(round(count(distinct b.usr_id) / count(distinct a.usr_id)*100, 2), '%') T1_retention_rate
from
tmp a
left join tmp b
on a.usr_id = b.usr_id
and datediff(b.dt, a.dt) = 1
group by 1
order by 1
with tmp as(
select usr_id, date(login_time) dt
from user_login_log
group by 1, 2
)
select
a.dt login_date,
concat(round(count(distinct b.usr_id) / count(distinct a.usr_id)*100, 2), '%') T1_retention_rate
from
tmp a
left join tmp b
on a.usr_id = b.usr_id
and datediff(b.dt, a.dt) = 1
where datediff(current_date(), a.dt) <=30
group by 1
order by 1
with tmp as(
select usr_id, date(login_time) dt
from user_login_log
group by 1, 2
)
select
a.dt login_date,
concat(round(avg(b.dt is not null)*100, 2), '%') T1_retention_rate
from
tmp a
left join tmp b
on a.usr_id = b.usr_id
and datediff(b.dt, a.dt) = 1
where datediff(current_date(), a.dt) <=30
group by 1
order by 1
with tmp as(
select usr_id, date(login_time) dt
from user_login_log
group by 1, 2
)
select
a.dt login_date,
concat(round(count(b.dt) *100/count(a.dt), 2), '%') T1_retention_rate
from
tmp a
left join tmp b
on a.usr_id = b.usr_id
and datediff(b.dt, a.dt) = 1
where datediff(current_date(), a.dt) <=30
group by 1
order by 1
with tmp as(
select usr_id, date(login_time) dt
from user_login_log
group by 1, 2
)
select
a.dt login_date,
concat(round(count(b.dt) *100/count(a.dt), 2), '%') T1_retention_rate
from
tmp a
left join tmp b
on a.usr_id = b.usr_id
and a.dt = date_sub(b.dt, interval 1 day)
where datediff(current_date(), a.dt) <=30
group by 1
order by 1
with tmp1 as(
select
a.usr_id,
a.v_id,
b.v_typ,
date(a.v_tm) v_dt
from bilibili_t20 a left join bilibili_t3 b on a.v_id = b.v_id
where date(a.v_tm) between '2021-02-05' and '2021-02-08'
)
select
a.v_typ,
count(distinct a.usr_id) total_views,
count(distinct b.usr_id) retained_users,
round(count(distinct b.usr_id) / count(distinct a.usr_id) *100, 2) retention_rate
from tmp1 a left join tmp1 b on a.usr_id = b.usr_idand datediff(b.v_dt, a.v_dt) between 1 and 3
where a.v_dt = '2021-02-05'
group by 1
order by 4 desc
with tmp1 as(
select
a.usr_id,
a.v_id,
b.v_typ,
date(a.v_tm) v_dt
from bilibili_t20 a left join bilibili_t3 b on a.v_id = b.v_id
where a.v_tm between '2021-02-05' and '2021-02-09'
)
select
a.v_typ,
count(distinct a.usr_id) total_views,
count(distinct b.usr_id) retained_users,
round(count(distinct b.usr_id) / count(distinct a.usr_id) *100, 2) retention_rate
from tmp1 a left join tmp1 b on a.usr_id = b.usr_idand datediff(b.v_dt, a.v_dt) between 1 and 3
where a.v_dt = '2021-02-05'
group by 1
order by 4 desc
with tmp1 as(
select
usr_id,
date_format(min(login_time),'%Y-%m-01') reg_dt
from user_login_log
where login_time >= '2024-01-01' AND login_time < '2025-01-01'
group by 1
),
tmp2 as(
select
usr_id,
date_format(login_time,'%Y-%m-01') dt
from user_login_log
where login_time >= '2024-01-01' AND login_time < '2025-01-01'
group by 1,2
)
select
a.reg_dt current_month ,
round(count(b.dt)*100 / count(a.reg_dt),2) t_plus_1_month_retention_rate
from tmp1 a left join tmp2 b
on a.usr_id = b.usr_id
and DATE_FORMAT(DATE_ADD(a.reg_dt, INTERVAL 1 MONTH), '%Y-%m-01') = b.dt
where a.reg_dt < '2024-12-01'
group by 1
order by 1
with tmp as(
select
usr_id,
date_format(login_time,'%Y-%m-01') dt
from user_login_log
wherelogin_time >= '2024-01-01' AND login_time < '2025-01-01'
group by 1,2
)
select
a.dt current_month ,
round(count(b.dt)*100 / count(a.dt),2) t_plus_1_month_retention_rate
from tmp a left join tmp b on a.usr_id = b.usr_id and date(a.dt) = date_sub(b.dt,interval 1 month)
where a.dt < '2024-12-01'
group by 1
order by 1
with tmp1 as(
select usr_id,date(login_time) dt
from user_login_log
where date(login_time) >= date_sub(current_date(),interval 30 day)
group by 1,2
)
select
a.dt first_login_date,
round(count(distinct case when datediff(b.dt,a.dt) = 1then a.usr_id end ) *100 / count(distinct a.usr_id) ,2) t_plus_1_retention_rate,
round(count(distinct case when datediff(b.dt,a.dt) = 3then a.usr_id end ) *100 / count(distinct a.usr_id) ,2) t_plus_3_retention_rate,
round(count(distinct case when datediff(b.dt,a.dt) = 7then a.usr_id end ) *100 / count(distinct a.usr_id) ,2) t_plus_7_retention_rate,
round(count(distinct case when datediff(b.dt,a.dt) = 14 then a.usr_id end ) *100 / count(distinct a.usr_id),2) t_plus_14_retention_rate
from tmp1 a
left join tmp1 b
on a.usr_id = b.usr_id
and datediff(b.dt,a.dt) BETWEEN 1 AND 14
group by 1
order by 1
with tmp1 as(
select usr_id,date(login_time) dt
from user_login_log
where date(login_time) >= date_sub(current_date(),interval 30 day)
group by 1,2
)
select
a.dt first_login_date,
round(count(distinct case when datediff(b.dt,a.dt) = 1 then 1 else 0 end ) *100/ count(distinct a.usr_id) ,2) t_plus_1_retention_rate,
round(count(distinct case when datediff(b.dt,a.dt) = 3 then 1 else 0 end ) *100/ count(distinct a.usr_id) ,2) t_plus_3_retention_rate,
round(count(distinct case when datediff(b.dt,a.dt) = 7 then 1 else 0 end ) *100/ count(distinct a.usr_id) ,2) t_plus_7_retention_rate,
round(count(distinct case when datediff(b.dt,a.dt) = 14 then 1 else 0 end )*100 /count(distinct a.usr_id),2) t_plus_14_retention_rate
from tmp1 a
left join tmp1 b
on a.usr_id = b.usr_id
and datediff(b.dt,a.dt) BETWEEN 1 AND 14
group by 1
order by 1
with tmp1 as(
select usr_id,date(login_time) dt
from user_login_log
where date(login_time) >= date_sub(current_date(),interval 90 day)
group by 1,2
)
select
a.dt first_login_date,
round(count(distinct case when datediff(b.dt,a.dt) BETWEEN 1 AND 3then a.usr_id end ) *100/ count(distinct a.usr_id) ,2) t_plus_3_retention_rate,
round(count(distinct case when datediff(b.dt,a.dt) BETWEEN 1 AND 7then a.usr_id end ) *100/ count(distinct a.usr_id) ,2) t_plus_7_retention_rate,
round(count(distinct case when datediff(b.dt,a.dt) BETWEEN 1 AND 14 then a.usr_id end )*100 /count(distinct a.usr_id),2) t_plus_14_retention_rate
from tmp1 a
left join tmp1 b
on a.usr_id = b.usr_id
and datediff(b.dt,a.dt) BETWEEN 1 AND 14
group by 1
order by 1