HQL刷题 50道
尚硅谷HQL刷题网站
答案
1.查询累积销量排名第二的商品
select sku_id
from (select sku_id, dense_rank() over (order by total desc) rn
from (select sku_id, sum(sku_num) total
from order_detail
group by sku_id) t1) t2
where rn = 2;
2.查询至少连续三天下单的用户
select user_id
from (select user_id,
create_date,
sum(if(diff > 1, 1, 0)) over (partition by user_id order by create_date) groups
from (select user_id,
create_date,
yestoday,
datediff(create_date, yestoday) diff from (select user_id,
create_date,
lead (create_date, 1, '1970-01-01') over (partition by user_id order by create_date) as yestoday from
(select user_id, create_date
from order_info
group by user_id, create_date)
t1) t2) t3) t4
group by user_id, groups
having count (*)>=3;
3.查询各品类销售商品的种类数及销量最高的商品
select t3.category_id,
cate.category_name,
t3.sku_id,
t3.name,
t3.order_num,
t3.sku_cnt
from (select category_id,
sku_id,
name,
order_num,
sku_cnt
from (select category_id,
sku_id,
name,
order_num,
count(distinct sku_id) over (partition by category_id) sku_cnt,
row_number() over (partition by category_id order by order_num desc) rn
from (select sku.category_id, sku.sku_id, sku.name, sum(od.sku_num) order_num
from order_detail od
join sku_info sku on od.sku_id = sku.sku_id
group by sku.category_id, sku.sku_id, sku.name) t1) t2
where rn = 1) t3
join category_info cate on t3.category_id = cate.category_id;
4 查询用户的累计消费金额及VIP等级
select user_id,
create_date,
sum_so_far,
case
when sum_so_far >= 0 and sum_so_far < 10000 then '普通会员'
when sum_so_far >= 10000 and sum_so_far < 30000 then '青铜会员'
when sum_so_far >= 30000 and sum_so_far < 50000 then '白银会员'
when sum_so_far >= 50000 and sum_so_far < 80000 then '黄金会员'
when sum_so_far >= 80000 and sum_so_far < 100000 then '白金会员'
when sum_so_far >= 100000 then '钻石会员' end vip_level
from (select user_id,
create_date,
sum(total) over (partition by user_id order by create_date) sum_so_far
from (select user_id, create_date, sum(total_amount) total from order_info group by user_id, create_date) t1
order by user_id, create_date) t2;
5 查询首次下单后第二天连续下单的用户比率
select concat(round(users * 1.0 / total * 100, 1), '%') as percentage
from (select count(distinct user_id) users, total
from (select user_id,
create_date,
first_value(create_date) over (partition by user_id order by create_date) first_day,
count(distinct user_id) over () total
from order_info) t1
where datediff(create_date, first_day) = 1
group by total) t2;
6 每个商品销售首年的年份、销售数量和销售金额
select sku_id, year, order_num, order_amount
from (select sku_id, year, order_num, order_amount, row_number() over (partition by sku_id order by year) rn
from (select sku_id, year(create_date) year, sum(sku_num) order_num, sum(sku_num * price) order_amount
from order_detail
group by sku_id, year(create_date)) t1) t2
where rn = 1;
7 筛选去年总销量小于100的商品
select sku_id, name, order_num
from (select sku.sku_id, sku.name, sum(sku_num) order_num
from order_detail od
join sku_info sku on sku.sku_id = od.sku_id
where year(od.create_date) = '2021'
and datediff(od.create_date, sku.from_date) >= 30
group by sku.sku_id, sku.name) t1
where order_num < 100;
8 查询每日新用户数
select login_date_first, count(distinct user_id) user_count
from (select user_id,
to_date(login_ts) login_date_first,
row_number() over (partition by user_id order by login_ts) rn
from user_login_detail) t1
where rn = 1
group by login_date_first;
9 统计每个商品的销量最高的日期
select sku_id,
create_date,
sum_num
from (select sku_id,
create_date,
sum_num,
row_number() over (partition by sku_id order by sum_num desc,create_date) rn
from (select sku_id, create_date, sum(sku_num) sum_num
from order_detail
group by sku_id, create_date) t1) t2
where rn = 1
or create_date = current_date();
10 查询销售件数高于品类平均数的商品
select sku_id,
name,
sum_num,
floor(avg_num) cate_avg_num
from (select sku_id,
name,
sum_num,
avg(sum_num) over (partition by category_id) avg_num
from (select sku.sku_id,
sku.name,
sku.category_id,
sum(od.sku_num) sum_num
from order_detail od
join sku_info sku
on od.sku_id = sku.sku_id
group by sku.sku_id, sku.name, sku.category_id) t1) t2
where sum_num > floor(avg_num);
11 用户注册、登录、下单综合统计
select t1.user_id,
to_date(t1.register_time) register_date,
t1.total_login_count,
t1.login_count_2021,
count(*) order_count_2021,
sum(od.total_amount) order_amount_2021
from (select distinct user_id,
first_value(login_ts) over (partition by user_id order by login_ts) register_time,
count(*) over (partition by user_id) total_login_count,
sum(if(year(login_ts) = '2021', 1, 0)) over (partition by user_id) login_count_2021
from user_login_detail) t1
join order_info od on od.user_id = t1.user_id
where year(od.create_date) = '2021'
group by t1.user_id, to_date(t1.register_time),
t1.total_login_count,
t1.login_count_2021;
12 查询指定日期的全部商品价格
select sku_id,
price
from (select sku_id,
cast(tmp_price as decimal(16, 2)) price,
row_number() over (partition by sku_id order by dt desc) rn
from (select t1.sku_id, nvl(t2.change_date, t1.from_date) dt, nvl(t2.new_price, t1.price) tmp_price
from (select sku_id, price, from_date
from sku_info
where from_date <= '2021-10-01') t1
left join
(select sku_id, new_price, change_date
from sku_price_modify_detail
where change_date <= '2021-10-01') t2 on t1.sku_id = t2.sku_id) t3) t4
where rn = 1;
13 即时订单比例
select cast(plan / total as decimal(16, 2)) percentage
from (select count(*) total, sum(if(order_date = custom_date, 1, 0)) plan
from (select user_id,
order_date,
custom_date,
row_number() over (partition by user_id order by order_date) rn
from delivery_info) t1
where rn = 1) t2;
14 向用户推荐朋友收藏的商品
select distinct ship.user1_id user_id, f1.sku_id
from friendship_info ship
join favor_info f1 on ship.user2_id = f1.user_id
left join favor_info f2 on f2.user_id = ship.user1_id and f2.sku_id = f1.sku_id
where f2.sku_id is null;
select user_id, sku_id
from (select distinct ship.user1_id user_id, f1.sku_id
from friendship_info ship
join favor_info f1 on ship.user2_id = f1.user_id
union all
select user_id, sku_id
from favor_info) t1
group by user_id, sku_id
having count(*) < 2;
15 查询所有用户的连续登录两天及以上的日期区间
select user_id, min(dt) start_date, max(dt) end_date
from (select user_id, dt, sum(if(diff > 1, 1, 0)) over (partition by user_id order by dt) nums
from (select user_id, dt, datediff(dt, yestoday) diff
from (select user_id, dt, lag(dt, 1, '1970-01-01') over (partition by user_id order by dt) yestoday
from (select user_id, to_date(login_ts) dt
from user_login_detail
group by user_id, to_date(login_ts)) t1) t2) t3) t4
group by user_id, nums
having count(*) > 1;
16 男性和女性每日的购物总金额统计
select od.create_date,
sum(if(u.gender = '男', od.total_amount, 0)) total_amount_male,
sum(if(u.gender = '女', od.total_amount, 0)) total_amount_female
from order_info od
join user_info u on od.user_id = u.user_id
group by od.create_date;
17 订单金额趋势分析
select create_date,
round(sum(total_amount) over (order by ts range between 172800 preceding and current row), 2) total_3d,
round(avg(total_amount) over (order by ts range between 172800 preceding and current row), 2) avg_3d
from (select create_date,
unix_timestamp(create_date, 'yyyy-MM-dd') ts,
sum(total_amount) total_amount
from order_info
group by create_date) t1;
18.购买过商品1和商品2但是没有购买商品3的顾客
select user_id
from (select distinct order_info.user_id, order_detail.sku_id
from order_info
join order_detail on order_info.order_id = order_detail.order_id
where order_detail.sku_id in (1, 2, 3)) t1
group by user_id
having sum(if(sku_id = 3, 3, 1)) = 2;
19 统计每日商品1和商品2销量的差值
select create_date,
sum(if(sku_id = 1, sku_num, 0)) - sum(if(sku_id = 2, sku_num, 0)) diff
from order_detail
where sku_id in (1, 2)
group by create_date;
20 查询出每个用户的最近三笔订单
select user_id,
order_id,
create_date
from (select user_id,
order_id,
create_date,
dense_rank() over (partition by user_id order by create_date desc) rn
from order_info) t1
where rn < 4;
21 查询每个用户登录日期的最大空档期
select user_id, max(datediff(future, dt)) max_diff
from (select user_id,
dt,
lead(dt, 1, '2021-10-10') over (partition by user_id order by dt) future
from (select distinct user_id, to_date(login_ts) dt from user_login_detail) t1) t2
group by user_id;
22 查询相同时刻多地登陆的用户
select user_id
from (select u1.user_id,
if(u1.login_ts <= u2.login_ts, if(u1.logout_ts >= u2.login_ts, if(u1.ip_address = u2.ip_address, 0, 1), 0),
0) num
from user_login_detail u1
join user_login_detail u2 on u1.user_id = u2.user_id and u1.login_ts != u2.login_ts) t2
group by user_id
having sum(num) > 0;
23 销售额完成任务指标的商品
select distinct sku_id
from (select sku_id, sum(if(diff > 1, 1, 0)) over (partition by sku_id order by dt) num
from (select sku_id, dt, (year(dt) - year(pass)) * 12 + month(dt) - month(pass) diff
from (select sku_id, dt, lag(dt, 1, '1970-01-01') over (partition by sku_id order by dt) pass
from (select sku_id, dt
from (select sku_id, trunc(create_date, 'MM') dt, sum(price * sku_num) total
from order_detail
group by sku_id, trunc(create_date, 'MM')) t1
-- 按题目的过滤条件 where not ((sku_id = 1 and total < 21000) or (sku_id = 2 and total < 10000))
where (sku_id = 1 and total >= 21000)
or (sku_id = 2 and total >= 10000)) t2) t3) t4) t5
group by sku_id, num
having count(*) > 1;
24 根据商品销售情况进行商品分类
select category, count(*) cn
from (select sku_id,
case
when total <= 5000 then '冷门商品'
when total <= 19999 then '一般商品'
else '热门商品' end category
from (select sku_id, sum(sku_num) total from order_detail group by sku_id) t1) t2
group by category;
25 各品类销量前三的所有商品 题目意思不明确 dense_rank() row_number()
select sku_id,
category_id
from (select sku_id,
category_id,
dense_rank() over (partition by category_id order by total desc) rn
from (select od.sku_id, sku.category_id, sum(sku_num) total
from order_detail od
join sku_info sku on od.sku_id = sku.sku_id
group by od.sku_id, sku.category_id) t1) t2
where rn < 4;
26 各品类中商品价格的中位数
select category_id,
cast(avg(price) as decimal(16, 2)) medprice
from (select category_id,
price,
row_number() over (partition by category_id order by price) rn,
count(*) over (partition by category_id) cn
from sku_info) t1
where rn in (ceil((cn + 1) * 0.5), floor((cn + 1) * 0.5))
group by category_id;
27 找出销售额连续3天超过100的商品
select distinct sku_id
from order_detail
where create_date in
(select distinct create_date
from (select create_date, count(*) over (order by nums) cnt
from (select create_date,
sum(if(diff > 1, 1, 0)) over (order by create_date) nums
from (select create_date,
datediff(create_date,
lag(create_date, 1, '1970-01-01') over (order by create_date)) diff
from (select create_date
from order_detail
group by create_date
having sum(sku_num) > 100) t1) t2) t3) t4
where cnt > 2);
28 查询有新注册用户的当天的新用户数量、新用户的第一天留存率
select first_login,
sum(if(diff = 0, 1, 0)) register,
cast((sum(if(diff = 1, 1.0, 0)) / sum(if(diff = 0, 1, 0))) as decimal(16, 2)) retention
from (select distinct user_id,
to_date(min(login_ts) over (partition by user_id)) first_login,
datediff(to_date(login_ts), to_date(min(login_ts) over (partition by user_id))) diff
from user_login_detail) t1
group by first_login;
29 求出商品连续售卖的时间区间
select sku_id,
min(create_date) start_date,
max(create_date) end_date
from (select sku_id,
create_date,
sum(if(diff > 1, 1, 0)) over (partition by sku_id order by create_date) nums
from (select sku_id,
create_date,
datediff(create_date,
lag(create_date, 1, '1970-01-01') over (partition by sku_id order by create_date)) diff
from (select distinct sku_id, create_date
from order_detail) t1) t2) t3
group by sku_id, nums;
30 登录次数及交易次数统计
select t1.user_id, t1.login_date, t1.login_count, nvl(t2.order_count, 0) order_count
from (select user_id, to_date(login_ts) login_date, count(*) login_count
from user_login_detail
group by user_id, to_date(login_ts)) t1
left join
(select user_id, order_date, count(*) order_count
from delivery_info
group by user_id, order_date) t2 on t1.user_id = t2.user_id and t1.login_date = t2.order_date;
31 按年度列出每个商品销售总额
select sku_id,
year(create_date) year_date,
cast(sum(sku_num * price) as decimal(16, 2)) sku_sum
from order_detail
group by sku_id, year(create_date);
32 某周内每件商品每天销售情况
select sku_id,
sum(if(dayofweek(create_date) = 2, sku_num, 0)) monday,
sum(if(dayofweek(create_date) = 3, sku_num, 0)) tuesday,
sum(if(dayofweek(create_date) = 4, sku_num, 0)) wednesday,
sum(if(dayofweek(create_date) = 5, sku_num, 0)) thursday,
sum(if(dayofweek(create_date) = 6, sku_num, 0)) friday,
sum(if(dayofweek(create_date) = 7, sku_num, 0)) saturday,
sum(if(dayofweek(create_date) = 1, sku_num, 0)) sunday
from order_detail
where create_date >= '2021-09-27'
and create_date <= '2021-10-03'
group by sku_id;
33 查看每件商品的售价涨幅情况(排除只有1次涨幅的)
select sku_id, price_change
from (select sku_id,
row_number() over (partition by sku_id order by change_date desc) rn,
count(*) over (partition by sku_id) cn,
new_price - (lead(new_price, 1, 0) over (partition by sku_id order by change_date desc)) price_change
from sku_price_modify_detail) t1
where rn = 1
and cn > 1;
34 销售订单首购和次购分析
-- 题目实际意思
select user_id, min(create_date) first_date, max(create_date) last_date, cn
from (select user_id,
create_date,
cn,
row_number() over (partition by user_id order by create_date) rn
from (select o.user_id, o.create_date, count(*) over (partition by o.user_id) cn
from sku_info sku
join order_detail od on sku.sku_id = od.sku_id
join order_info o on o.order_id = od.order_id
where sku.name in ('xiaomi 10', 'apple 12', 'xiaomi 13')) t1
where cn > 1) t2
where rn < 3
group by user_id, cn;
-- 实际结果
select user_id, min(create_date) first_date, max(create_date) last_date, cn
from (select user_id, create_date, cn
from (select o.user_id, o.create_date, count(*) over (partition by o.user_id) cn
from sku_info sku
join order_detail od on sku.sku_id = od.sku_id
join order_info o on o.order_id = od.order_id
where sku.name in ('xiaomi 10', 'apple 12', 'xiaomi 13')) t1
where cn > 1) t2
group by user_id, cn;
35 同期商品售卖分析表
select sku_id,
month(create_date) month,
sum(if(year(create_date) = 2020, sku_num, 0)) 2020_skusum,
sum(if(year(create_date) = 2021, sku_num, 0)) 2021_skusum
from order_detail
-- 按题目意思 where create_date >= '2021-01-01' and create_date < '2023-01-01'
where create_date >= '2020-01-01'
and create_date < '2022-01-01'
group by sku_id, month(create_date);
36 国庆期间每个品类的商品的收藏量和购买量
select t1.sku_id, t1.total sku_sum, nvl(t2.uv, 0) favor_cn
from (select sku_id, sum(sku_num) total
from order_detail
where create_date >= '2021-10-01'
and create_date <= '2021-10-07'
group by sku_id) t1
left join
(select sku_id, count(distinct user_id) uv
from favor_info
where create_date >= '2021-10-01'
and create_date <= '2021-10-07'
group by sku_id) t2 on t1.sku_id = t2.sku_id;
37 统计活跃间隔对用户分级结果
select level, count(*) cn
from (select case
when datediff(today, register) <= 7 then '新增用户'
when datediff(today, login) <= 7 then '忠实用户'
when datediff(today, login) < 30 then '沉睡用户'
else '流失用户' end level
from (select distinct user_id,
max(dt) over () today,
min(dt) over (partition by user_id) register,
max(dt) over (partition by user_id) login
from (select distinct user_id, to_date(login_ts) dt
from user_login_detail) t1) t2) t3
group by level;
38 连续签到领金币数
select user_id,
sum(if(total % 7 > 2, floor(total / 7) * 15 + (total % 7) + 2, floor(total / 7) * 15 + (total % 7))) sum_coin_cn
from (select user_id, count(*) total
from (select user_id,
sum(if(nums > 1, 1, 0)) over (partition by user_id order by dt) type
from (select user_id,
dt,
datediff(dt, lag(dt, 1, '1970-01-01') over (partition by user_id order by dt)) nums
from (select distinct user_id, to_date(login_ts) dt
from user_login_detail) t1) t2) t3
group by user_id, type) t4
group by user_id
order by sum_coin_cn desc;
39 国庆期间的7日动销率和滞销率
-- 固定式(要优化)
select category_id,
cast(round(first / first_total, 2) as decimal(16, 2)) first_sale_rate,
cast(1 - round(first / first_total, 2) as decimal(16, 2)) first_unsale_rate,
cast(round(second / second_total, 2) as decimal(16, 2)) second_sale_rate,
cast(1 - round(second / second_total, 2) as decimal(16, 2)) second_unsale_rate,
cast(round(third / third_total, 2) as decimal(16, 2)) third_sale_rate,
cast(1 - round(third / third_total, 2) as decimal(16, 2)) third_unsale_rate,
cast(round(fourth / fourth_total, 2) as decimal(16, 2)) fourth_sale_rate,
cast(1 - round(fourth / fourth_total, 2) as decimal(16, 2)) fourth_unsale_rate,
cast(round(fifth / fifth_total, 2) as decimal(16, 2)) fifth_sale_rate,
cast(1 - round(fifth / fifth_total, 2) as decimal(16, 2)) fifth_unsale_rate,
cast(round(sixth / sixth_total, 2) as decimal(16, 2)) sixth_sale_rate,
cast(1 - round(sixth / sixth_total, 2) as decimal(16, 2)) sixth_unsale_rate,
cast(round(seventh / seventh_total, 2) as decimal(16, 2)) seventh_sale_rate,
cast(1 - round(seventh / seventh_total, 2) as decimal(16, 2)) seventh_unsale_rate
from (select sku.category_id,
count(distinct if(sku.from_date <= '2021-10-01', sku.sku_id, null)) first_total,
count(distinct if(od.create_date = '2021-10-01', od.sku_id, null)) first,
count(distinct if(sku.from_date <= '2021-10-02', sku.sku_id, null)) second_total,
count(distinct if(od.create_date = '2021-10-02', od.sku_id, null)) second,
count(distinct if(sku.from_date <= '2021-10-03', sku.sku_id, null)) third_total,
count(distinct if(od.create_date = '2021-10-03', od.sku_id, null)) third,
count(distinct if(sku.from_date <= '2021-10-04', sku.sku_id, null)) fourth_total,
count(distinct if(od.create_date = '2021-10-04', od.sku_id, null)) fourth,
count(distinct if(sku.from_date <= '2021-10-05', sku.sku_id, null)) fifth_total,
count(distinct if(od.create_date = '2021-10-05', od.sku_id, null)) fifth,
count(distinct if(sku.from_date <= '2021-10-06', sku.sku_id, null)) sixth_total,
count(distinct if(od.create_date = '2021-10-06', od.sku_id, null)) sixth,
count(distinct if(sku.from_date <= '2021-10-07', sku.sku_id, null)) seventh_total,
count(distinct if(od.create_date = '2021-10-07', od.sku_id, null)) seventh
from sku_info sku
left join order_detail od on sku.sku_id = od.sku_id
group by sku.category_id) t1;
40 出平台同时在线最多的人数
select max(num) as cn
from (select sum(flag) over (order by dt) num
from (select login_ts dt, 1 flag
from user_login_detail
union all
select logout_ts dt, -1 flag
from user_login_detail) t1) t2;
41 同时在线人数问题
select live_id, max(num) max_user_count
from (select live_id, sum(flag) over (partition by live_id order by dt) num
from (select user_id, live_id, in_datetime dt, 1 flag
from live_events
union all
select user_id, live_id, out_datetime dt, -1 flag
from live_events) t1) t2
group by live_id;
42 会话划分问题
select user_id,
page_id,
view_timestamp,
concat(user_id, '-', sum(if(diff > 60, 1, 0)) over (partition by user_id order by view_timestamp)) session_id
from (select user_id,
page_id,
view_timestamp,
view_timestamp - lag(view_timestamp, 1, 0) over (partition by user_id order by view_timestamp) diff
from page_view_events) t1;
43 间断连续登录用户问题
select user_id, max(num) max_day_count
from (select user_id, datediff(max(dt), min(dt)) + 1 num
from (select user_id, dt, sum(if(diff > 2, 1, 0)) over (partition by user_id order by dt) type
from (select user_id,
dt,
datediff(dt, lag(dt, 1, '1970-01-01') over (partition by user_id order by dt)) diff
from (select distinct user_id, to_date(login_datetime) dt
from login_events) t1) t2) t3
group by user_id, type) t4
group by user_id;
44 日期交叉问题
select brand, sum(if(datediff(end_date, stt) >= 0, datediff(end_date, stt) + 1, 0)) promotion_day_count
from (select brand,
if(max_date is null, start_date, if(start_date > max_date, start_date, date_add(max_date, 1))) stt,
end_date
from (select brand,
start_date,
end_date,
max(end_date)
over (partition by brand order by start_date rows between UNBOUNDED PRECEDING and 1 PRECEDING) max_date
from promotion_info) t1) t2
group by brand;
45 复购率问题(注意全是90天内)
select product_id, cast(sum(if(nums > 1, 1, 0)) / count(*) as decimal(16, 2)) as cpr
from (select user_id, product_id, count(*) nums
from (select user_id, product_id, datediff(max(order_date) over (), order_date) diff
from order_detail) t1
where diff <= 90
group by user_id, product_id) t2
group by product_id
order by crp desc, product_id;
46 出勤率问题
select course_id,
cast(sum(if(total is null, 0, if(total > 2400, 1, 0))) / count(*) as decimal(16, 2)) adr
from (select t1.course_id, sum(unix_timestamp(l.login_out) - unix_timestamp(l.login_in)) total
from (select course_id, id from course_apply lateral view explode(user_id) user_id as id) t1
left join user_login l on t1.course_id = l.course_id and l.user_id = t1.id
group by t1.course_id, t1.id) t2
group by course_id;
47 打车问题
select period,
count(*) get_car_num,
cast(avg(nvl(wait, 0)) / 60 as decimal(16, 2)) wait_time,
cast(avg(nvl(dispatch, 0)) / 60 as decimal(16, 2)) dispatch_time
from (select case
when hour(r.event_time) >= 7 and hour(r.event_time) < 9 then '早高峰'
when hour(r.event_time) >= 9 and hour(r.event_time) < 17 then '工作时间'
when hour(r.event_time) >= 17 and hour(r.event_time) < 20 then '晚高峰'
else '休息时间' end period,
unix_timestamp(o.order_time) - unix_timestamp(r.event_time) wait,
unix_timestamp(o.start_time) - unix_timestamp(o.order_time) dispatch
from get_car_record r
left join get_car_order o on r.order_id = o.order_id) t1
group by period;
48 排列问题
-- 自连接
select t1.team_name team_name_1, t2.team_name team_name_2
from team t1
join team t2 on t1.team_name > t2.team_name;
-- 开窗聚合,炸裂函数
select team_name_1, team_name_2
from (select team_name team_name_1,
collect_list(team_name)
over (order by team_name rows between 1 following and unbounded following) team_list
from team) t2 lateral view explode(team_list) team_list as team_name_2;
49 视频热度问题
-- 结果(但是不符合题目意思)
select video_id,
cast(ceil((whole / total + up + comment + retweet) / (datediff(today, max_dt) + 1)) as decimal(16, 1)) heat
from (select video_id,
today,
max(dt) max_dt,
count(*) total,
sum(if(l.ts >= i.duration, 1, 0)) * 100 whole,
sum(l.if_like) * 5 up,
count(l.comment_id) * 3 comment,
sum(l.if_retweet) * 2 retweet
from (select video_id,
unix_timestamp(end_time) - unix_timestamp(start_time) ts,
to_date(end_time) dt,
to_date(max(end_time) over (partition by video_id)) today,
if_like,
comment_id,
if_retweet
from user_video_log) l
join video_info i on i.video_id = l.video_id
where l.dt <= l.today
and l.dt >= date_sub(l.today, 29)
group by l.video_id, today) t1
order by heat
limit 3;
-- 题目意思
select video_id,
cast(((whole / total + up + comment + retweet) / fresh) as decimal(16, 2)) heat
from (select video_id,
30 - count(distinct dt) + 1 fresh,
count(*) total,
sum(if(l.ts >= i.duration, 1, 0)) * 100 whole,
sum(l.if_like) * 5 up,
count(l.comment_id) * 3 comment,
sum(l.if_retweet) * 2 retweet
from (select video_id,
unix_timestamp(end_time) - unix_timestamp(start_time) ts,
to_date(end_time) dt,
to_date(max(end_time) over ()) today,
if_like,
comment_id,
if_retweet
from user_video_log) l
join video_info i on i.video_id = l.video_id
where l.dt <= l.today
and l.dt >= date_sub(l.today, 29)
group by l.video_id) t1
order by heat
limit 3;
50 员工在职人数问题
select mth,cast(sum(num) as decimal(16,2)) ps from
(select month(dt) mth,id,sum(if((dt >= en_dt and dt <= le_dt)
or (dt >= en_dt and le_dt is null),1,0))/count(*) num
from cal join
emp
where dt < '2020-04-01'
and dt >= '2020-01-01' group by month(dt),id) t2 group by mth;