目录
1. 各品类销量前三的所有商品
1.1 题目需求
从订单详情表中(order_detail)和商品表(sku_info)中查询各个品类销售数量前三的商品。如果该品类小于三个商品,则输出所有的商品销量。
Sku_id(商品id) | Category_id(品类id) |
---|---|
2 | 1 |
4 | 1 |
1 | 1 |
8 | 2 |
7 | 2 |
5 | 2 |
12 | 3 |
11 | 3 |
10 | 3 |
1.2 代码实现
hive>
select
t2.sku_id,
t2.category_id
from
(
select
t1.sku_id,
si.category_id,
rank() over(partition by category_id order by t1.sku_sum desc) rk
from
(
select
sku_id,
sum(sku_num) sku_sum
from
order_detail
group by
sku_id
)t1
join
sku_info si
on
t1.sku_id = si.sku_id
)t2
where
t2.rk <= 3;
标签:sku,10,t2,Hive,t1,商品,SQL,品类,id
From: https://blog.csdn.net/qq_45115959/article/details/142301226