Hive综合案例练习(中级)第十题:查询销售件数高于品类平均数的商品

查询销售件数高于品类平均数的商品

题目需求

从订单明细表(order_detail)中查询累积销售件数高于其所属品类平均数的商品,期望结果如下:

sku_id name sum_num cate_avg_num
2 手机壳 302 110.5
5 破壁机 242 194.75
7 热水壶 252 194.75
8 微波炉 253 194.75
10 帐篷 299 290.5
11 烧烤架 320 290.5
12 遮阳伞 349 290.5

代码实现

select 
	sku_id,
	name,
	sum_num,
	cate_avg_num
from
(
	select 
		t1.sku_id,
		si.name,
		t1.sum_num,
		avg(t1.sum_num) over (partition by si.category_id) cate_avg_num,
		(t1.sum_num - avg(t1.sum_num) over (partition by si.category_id)) flag
	from 
	(
		select 
			sku_id,
			sum(sku_num) sum_num
		from order_detail
		group by sku_id 
	)t1
	left join sku_info si 
	on t1.sku_id=si.sku_id 
)t2
where flag > 0;

set hive.strict.checks.type.safety=false;
set hive.mapred.mode=nostrict;
select sku_id,
       name,
       sum_num,
       cate_avg_num
from (
         select od.sku_id,
                category_id,
                name,
                sum_num,
                avg(sum_num) over (partition by category_id) cate_avg_num
         from (
                  select sku_id,
                         sum(sku_num) sum_num
                  from order_detail
                  group by sku_id
              ) od
                  left join
              (
                  select sku_id,
                         name,
                         category_id
                  from sku_info
              ) sku
              on od.sku_id = sku.sku_id) t1
where sum_num > cate_avg_num;

你可能感兴趣的:(Hive综合案例练习,hive,hadoop,大数据)