1. 数据结构:数据为平台商品促销数据
表名:good_promotion
字段名:brand(品牌)、stt(打折开始日期)、edt(打折结束日期)。
2. 需求:
① 创建表
② 计算每个商品总的打折销售天数
注意:其中的交叉日期。比如vivo品牌,第一次活动时间为2021-06-05到2021-06-15,第二次活动时间为2021-06-09到2021-06-21其中9号到15号为重复天数,只统计一次,即vivo总打折天数为2021-06-05到2021-06-21共计17天。
3. 数据准备:
编辑数据文件good_promotion.txt
[at@hadoop102 ~]$ vim /opt/module/hive/datas/good_promotion.txt
oppo 2021-06-05 2021-06-09
oppo 2021-06-11 2021-06-21
vivo 2021-06-05 2021-06-15
vivo 2021-06-09 2021-06-21
redmi 2021-06-05 2021-06-21
redmi 2021-06-09 2021-06-15
redmi 2021-06-17 2021-06-26
huawei 2021-06-05 2021-06-26
huawei 2021-06-09 2021-06-15
huawei 2021-06-17 2021-06-21
create table good_promotion(
brand string,
stt string,
edt string
)
row format delimited
fields terminated by '\t';
sql">selectbrand,sum(if(days>=0,days+1,0)) days
from (selectbrand,datediff(edt,stt) daysfrom (selectbrand,if(maxEdt is null,stt,if(stt>maxEdt,stt,date_add(maxEdt,1))) stt,edtfrom (selectbrand,stt,edt,max(edt) over(partition by brand order by stt rows between UNBOUNDED PRECEDING and 1 PRECEDING) maxEdtfrom good_promotion)t1)t2
)t3
group by brand;