withtempas(select
tag
,if(timestampdiff(second,start_time,end_time)/duration*100>100,100,timestampdiff(second,start_time,end_time)/duration*100)as play_progress
from tb_video_info
leftjoin tb_user_video_log using(video_id)where start_time isnotnull)select
tag
,concat(round(avg(play_progress),2),'%') avg_play_progress
fromtempgroupby tag
havingavg(play_progress)>60orderby avg_play_progress desc;
SQL3 每类视频近一个月的转发量/率
withtempas(select
tag
,if_retweet
,if(start_time,1,0) if_hudong
,date_format(start_time,'%Y%m%d') now_time
,date_format(date_sub(max(date_format(start_time,'%Y%m%d'))over(),interval29day),'%Y%m%d')as starttime
,max(date_format(start_time,'%Y%m%d'))over()as endtime
from tb_video_info
leftjoin tb_user_video_log using(video_id)where start_time isnotnull)select
tag
,sum(if_retweet) retween_cnt
,round(sum(if_retweet)/sum(if_hudong),3) retweet_rate
fromtempwhere now_time between starttime and endtime
groupby tag
orderby retweet_rate desc;
select
date_format(event_time,'%Y-%m')asmonth,sum(if(total_amount>0,total_amount,0))as GMV
from tb_order_overall
whereyear(event_time)='2021'groupbymonthhaving GMV>100000orderby GMV
SQL14 统计2021年10月每个退货率不大于0.5的商品各项指标
select
product_id
,round(avg(if_click),3)as ctr
,round(sum(if_cart)/sum(if_click),3)as cart_rate
,round(sum(if_payment)/sum(if_cart),3)as payment_rate
,round(sum(if_refund)/sum(if_payment),3)as refund_rate
from tb_user_event
where date_format(event_time,'%Y%m')='202110'groupby product_id
orderby product_id asc
SQL15 某店铺的各商品毛利率及店铺整体毛利率
(select'店铺汇总'as product_id
-- ,sum(d.price*d.cnt) -- 销售总额-- ,sum(d.cnt*i.in_price) -- 进价总额,concat(round((1-sum(d.cnt*i.in_price)/sum(d.price*d.cnt))*100,1),'%')as profit_rate
from tb_product_info i
leftjoin tb_order_detail d on i.product_id=d.product_id
leftjoin tb_order_overall o on o.order_id=d.order_id
where i.shop_id='901'and date_format(event_time,'%Y%m')>='202110'groupby i.shop_id)unionall(select
d.product_id as product_id
-- ,sum(d.price*d.cnt) -- 销售总额-- ,sum(d.cnt*i.in_price) -- 进价总额,concat(round((1-sum(d.cnt*i.in_price)/sum(d.price*d.cnt))*100,1),'%')as profit_rate
from tb_product_info i
leftjoin tb_order_detail d on i.product_id=d.product_id
leftjoin tb_order_overall o on o.order_id=d.order_id
where i.shop_id='901'and date_format(event_time,'%Y%m')>='202110'groupby d.product_id
having(1-sum(d.cnt*i.in_price)/sum(d.price*d.cnt))*100>24.9orderby product_id
)
SQL16零食类商品中复购率top3高的商品
withtempas(select
i.product_id as product_id -- 商品ID,o.uid as uid -- 用户ID,count(*)as cnt -- 商品被用户购买次数from tb_product_info i
leftjoin tb_order_detail d on i.product_id=d.product_id
leftjoin tb_order_overall o on o.order_id=d.order_id
where i.tag='零食'anddate(o.event_time)>=(selectdate(date_sub(max(event_time),interval89day))from tb_order_overall)groupby i.product_id,o.uid
)select
product_id
,round(avg(casewhen cnt>=2then1else0end),3)as repurchase_rate
fromtempgroupby product_id
orderby repurchase_rate desc,product_id asclimit3
SQL17 10月的新户客单价和获客成本
selectround(sum(total_amount)/count(order_id),1) avg_amount,round(avg(cost),1) avg_cost
from(select a.order_id,
total_amount,(sum(price*cnt)- total_amount)as cost
from tb_order_detail a
leftjoin tb_order_overall b
on a.order_id = b.order_id
where date_format(event_time,'%Y-%m')='2021-10'and(uid,event_time)in(select uid ,min(event_time)-- 用户和其第一次购买的时间from tb_order_overall
GROUPBY uid )GROUPBY a.order_id) a
SQL18 店铺901国庆期间的7日动销率和滞销率
with a as(selectdistinctdate(event_time) dt
from tb_order_overall
wheredate(event_time)between'2021-10-01'and'2021-10-03'),b as(selectdate(too.event_time) dt, tod.product_id
from tb_order_detail tod join tb_order_overall too on tod.order_id = too.order_id
join tb_product_info tpi on tod.product_id = tpi.product_id
where tpi.shop_id =901and too.status=1)select
a.dt
,round(count(distinct b.product_id)/(selectcount(product_id)from tb_product_info wheredate(release_time)<=a.dt and shop_id=901),3) sale_rate
,round(1-(count(distinct b.product_id)/(selectcount(product_id)from tb_product_info wheredate(release_time)<=a.dt and shop_id=901)),3)as unsale_rate
from a leftjoin b on datediff(a.dt,b.dt)between0and6groupby a.dt
orderby a.dt
SQL19 2021年国庆在北京接单3次及以上的司机统计信息
withtempas(select
driver_id
,count(order_id) order_num
,sum(fare) income
from tb_get_car_record
leftjoin tb_get_car_order using(order_id)wheredate(event_time)>='2021-10-01'anddate(finish_time)<='2021-10-07'and finish_time isnotnulland city='北京'groupby driver_id
havingcount(driver_id)>=3)select'北京'as city
,round(avg(order_num),3)as avg_order_num
,round(avg(income),3)as avg_income
fromtemp
SQL20 有取消订单记录的司机平均评分
selectcoalesce(driver_id,"总体"),round(sum(grade)/count(grade),1)as avg_grade
from tb_get_car_order
where driver_id in(select driver_id
from tb_get_car_order
where start_time isnulland date_format(order_time,'%Y%m')='202110')groupby driver_id with rollup
SQL21 每个城市中评分最高的司机信息
select
t3.city
,t3.driver_id
,round(avg(tb_get_car_order.grade),1)as avg_grade
,round(count(1)/count(distinctdate(order_time)),1)as avg_order_num
,round(sum(mileage)/count(distinctdate(order_time)),3)as avg_mileage
from(select
city
,driver_id
from(select
city
,driver_id
,dense_rank()over(partitionby city orderby grade desc) rk
from(select
city
,driver_id
,avg(grade)as grade
from tb_get_car_record
join tb_get_car_order using(order_id)where fare isnotnullgroupby city,driver_id
) t1
) t2
where rk=1) t3 leftjoin tb_get_car_order on t3.driver_id=tb_get_car_order.driver_id
groupby city,driver_id
orderby avg_order_num asc
SQL22 国庆期间近7日日均取消订单量
select
dt, finish_num_7d, cancel_num_7d
from(select
dt
,round(avg(finish)over(orderby dt rows6preceding),2)as finish_num_7d
,round(avg(cancel)over(orderby dt rows6preceding),2)as cancel_num_7d
from(selectdate(order_time)as dt
,count(start_time)as finish
,count(1)-count(start_time)as cancel
from tb_get_car_order
groupbydate(order_time)) t
) t1
where dt>='2021-10-01'and dt<='2021-10-03'
with a as(select city,uid,event_time as dt,1as diff
from tb_get_car_record
where date_format(event_time,'%Y-%m')='2021-10'unionselect city,uid,end_time as dt,-1as diff
from tb_get_car_record
where date_format(event_time,'%Y-%m')='2021-10'and order_id isnullunionselect city,tb_get_car_order.uid,finish_time as dt,-1as diff
from tb_get_car_order
leftjoin tb_get_car_record using(order_id)where date_format(event_time,'%Y-%m')='2021-10'and fare isnullunionselect city,tb_get_car_order.uid,start_time as dt,-1as diff
from tb_get_car_order
leftjoin tb_get_car_record using(order_id)where date_format(event_time,'%Y-%m')='2021-10')select city,max(num)as number
from(select
city,sum(diff)over(partitionby city,date_format(dt,'%Y-%m')orderby dt asc,diff desc)as num
from a
) t
groupby city
orderby number asc
with a as(select
style_id
,sum(inventory)as total_inventory
,sum(tag_price*inventory)as inventory_price
from product_tb
groupby1),
b as(select
style_id
,sum(sales_price) GMV
,sum(sales_num) sales_num_total
from sales_tb
leftjoin product_tb using(item_id)groupby style_id
)select
style_id
,round(sales_num_total/(total_inventory-sales_num_total)*100,2) pin_rate
,round(GMV/inventory_price*100,2) sell_through_rate
from a join b using(style_id)orderby1
with a as(select course_id,sum(if_sign)as sign_total
from behavior_tb
groupby course_id
),b as(select course_id,count(distinct user_id) total_10
from attend_tb
where timestampdiff(minute,in_datetime,out_datetime)>=10groupby course_id
)select te.course_id,t.course_name,te.rate
from(select a.course_id as course_id,round(total_10/sign_total*100,2)as rate
from a leftjoin b on a.course_id=b.course_id
) te join course_tb t on t.course_id=te.course_id
SQL34 牛客直播各科目同时在线人数
select course_id,course_name,num_max
from(select course_id,max(num)as num_max
from(select
course_id
,sum(diff)over(partitionby course_id orderby df asc,diff desc)as num
from(select user_id,course_id,in_datetime as df,1as diff
from attend_tb
unionselect user_id,course_id,out_datetime as df,-1as diff
from attend_tb
) t
) t1
groupby course_id
) t2 join course_tb using(course_id)
SQL35 某乎问答11月份日人均回答量
selectdate(answer_date),round(count(issue_id)/count(distinct author_id),2)from answer_tb
where date_format(answer_date,'%Y%m')='202111'groupbydate(answer_date)orderbydate(answer_date)
SQL36 某乎问答高质量的回答中用户属于各级别的数量
selectcasewhen author_level between1and2then'1-2级'when author_level between3and4then'3-4级'when author_level between5and6then'5-6级'endas level_cut,count(issue_id)as num
from author_tb leftjoin answer_tb using(author_id)where char_len>=100groupby level_cut
orderby num desc
SQL37 某乎问答单日回答问题数大于等于3个的所有用户
selectdate(answer_date),author_id
,count(1)as answer_cnt
from answer_tb
where date_format(answer_date,'%Y%m')='202111'groupbydate(answer_date),author_id
having answer_cnt>=3orderbydate(answer_date),author_id
SQL38 某乎问答回答过教育类问题的用户里有多少用户回答过职场类问题
selectcount(distinct author_id)from answer_tb
where author_id in(select author_id
from answer_tb
where issue_id in(select issue_id
from issue_tb
where issue_type='Education'))and issue_id in(select issue_id
from issue_tb
where issue_type='Career')
SQL39 某乎问答最大连续回答问题天数大于等于3天的用户及其对应等级
select author_id ,author_level,num
from(select
author_id ,author_level,date_sub(answer_date,interval rn day),count(*)as num
from(select
author_id
,author_level
,answer_date
,row_number()over(partitionby author_id orderby answer_date) rn
from answer_tb
leftjoin author_tb using(author_id)) t
groupby author_id ,author_level,date_sub(answer_date,interval rn day)havingcount(*)>=3) t1