,casewhen array_contains(collect_set(promotion_behavior) over (partitionby sales_member_id,sales_order_number),'大促时购买') then'private'else'normal'endas promotion_behavior
,casewhen array_contains(collect_set(promotion_behavior),'private') and!array_contains(collect_set(promotion_behavior),'normal') then'只在大促购买' when array_contains(collect_set(promotion_behavior),'normal') and!array_contains(collect_set(promotion_behavior),'private') then'只在非大促购买'else'大促及非大促都购买'endas private_sale_sensitivity
节日
1 2 3 4
where to_date(place_time) between date_sub(concat(year(current_date),'-','01-01'),7) and date_add(concat(year(current_date),'-','01-01'),7)--New Year or to_date(place_time) between date_sub(concat(year(current_date),'-','02-14'),7) and date_add(concat(year(current_date),'-','02-14'),7)--Valentine or to_date(place_time) between date_sub(concat(year(current_date),'-','{Qixi}'),7) and date_add(concat(year(current_date),'-','{Qixi}'),7)--Qixi or to_date(place_time) between date_sub(concat(year(current_date)-1,'-','12-25'),7) and date_add(concat(year(current_date)-1,'-','12-25'),7)--Christmas
select sku_cd,category,sap_price ,casewhen rn>=0and rn<0.3then N'低价' when rn>=0.3and rn<0.7then N'中价' when rn>=0.7and rn<=1then N'高价'elsenullendas price_tier_type into #sku_price_tier from( select sku_cd,category,sap_price ,round(PERCENT_RANK() OVER( partitionby category ORDERBY sap_price), 5) as rn from #sku_price where category isnotnull )t1
取下一行
1
,lead(channel,1,null) over(partitionby account_id orderby sap_time) as next_channel
取上一行
1
,lag(channel,1,null) over(partitionby account_id orderby sap_time) as next_channel
like
1
casewhen XX like'%礼盒%'then1else0endas if_gift
转数字类型+ 文本格式化
1
cast(regexp_replace(member_card,'JD','') asbigint) as member_card
hive 首字母大写
1 2 3
,casewhen member_card_grade in ('PINK','WHITE','BLACK','GOLD') then concat(substr(member_card_grade, 1, 1), lower(substr(member_card_grade, 2))) elsenullendas member_card_grade
文本筛选 判断是数字并且不为空
1 2
where (user_id rlike '^\\d+$'or user_id isnullor user_id in ('null','NULL',' ','')) and (card_no rlike '^\\d+$'or card_no isnullor card_no in ('null','NULL',' ',''))
字符串拼接
1 2 3 4 5
,casewhen chcnt =1then concat(store,'新客') when chcnt >1then concat(store,'老客') else concat(store,'未购买过') endas channel_status
select sales_member_id ,max(casewhen item_brand_name ='Lancome'then sales else0end) as lancome_sales ,max(casewhen item_brand_name ='Lauder'then sales else0end) as lauder_sales ,max(casewhen item_brand_name ='Guerlain'then sales else0end) as guerlain_sales ,max(casewhen item_brand_name ='Skii'then sales else0end) as skll_sales ,max(casewhen item_brand_name ='Shiseido'then sales else0end) as shiseido_sales from ( select sales_member_id,item_brand_name ,sum(item_apportion_amount) as sales from da_dev.tagging_system_sales_order_vb_temp groupby sales_member_id,item_brand_name ) t1 groupby sales_member_id
分组 列拼接
1 2 3 4 5 6 7 8 9 10
select sales_member_id,concat_ws(',',collect_set(item_category)) as dragon_registered_category from( select sales_member_id,item_category from ( select sales_member_id,sales_order_number,item_category ,row_number() over(partitionby sales_member_id,sales_order_number orderby place_time) as rn from DA.tagging_system_sales_order_basic_temp where store='丝芙兰官网' ) t1 where rn=1 ) tt1 groupby sales_member_id