尚硅谷HQL刷题网站
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;
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;
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;
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;
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;
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;
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;
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;
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();
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);
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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);
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;
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;
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;
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);
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;
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;
-- 题目实际意思
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;
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);
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;
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;
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;
-- 固定式(要优化)
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;
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;
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;
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;
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;
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;
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;
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;
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;
-- 自连接
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;
-- 结果(但是不符合题目意思)
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;
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;