hive 实例

该博客通过Hive查询展示了如何获取2015年每个用户最初的两次订单详情,包括每个订单中购买的商品编号列表,提供了数据处理的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.得出2015年每个用户前2次订单,并得出每个订单购买的商品号列表 

select *
from (
select 
cust_id,parent_id,concat_ws('#', collect_set(product_id)),row_number() over (partition by cust_id order by order_creation_date) as paiming
from dwfact.order_send_detail orders
where orders.data_date >= '2015-01-01' and data_date < '2016-01-01'
and cust_id = '9680935'
group by cust_id,parent_id,order_creation_date
) b
where paiming < 3 ;

2.得到2015年6月1日的前30天、前60天、前90天的金额汇总

select
data_date, sum(moneys) over(order by data_date rows between 30 preceding and CURRENT ROW)
from 
(
select data_date,sum(bargin_price*allot_quantity) as moneys
from 
dwfact.order_send_detail orders
where data_date >= '2015-01-01' and data_date < '2015-05-01'
group by data_date
) a;
如果需求是每一天的前60天这个还可以。

建议使用下面的:
select 
sum(case when datediff('2015-06-01',data_date) <= 30 then  bargin_price*allot_quantity else 0 end) as 30_moneys,
sum(case when datediff('2015-06-01',data_date) <= 60 then  bargin_price*allot_quantity else 0 end) as 60_moneys,
sum(case when datediff('2015-06-01',data_date) <= 90 then  bargin_price*allot_quantity else 0 end) as 90_moneys
from dwfact.order_send_detail orders
where data_date >= '2015-01-01' and data_date <= '2015-06-01';

3.获得2014年、2015年销售最高和最低的月份及销售额

select 
substr(data_date,0,4) as year, 
substr(data_date,0,7) as month,
sum(bargin_price*allot_quantity) as moneys,
row_number() over (partition by substr(data_date,0,4) order by sum(bargin_price*allot_quantity) desc) as number,
row_number() over (partition by substr(data_date,0,4) order by sum(bargin_price*allot_quantity)) as number2
from dwfact.order_send_detail
where data_date >= '2014-01-01' and data_date < '2016-01-01'
group by substr(data_date,0,4),substr(data_date,0,7);

4.一个字段的值为123456789_a232323_b3434343_s343434_r43434
规则为一串数字与其他首字符为字母其余为数字的单元用_连接,请问怎么获得第一串数字,和s开头的后面的数字,首字母s的数字串位置和数量都不固定

select 
regexp_extract('123456789_a232323_b3434343_23456_s343434_r43434','^(\\d+)',1),
regexp_extract('123456789_a232323_b3434343_23456_s343434_s43434','_([s])(\\d+)',2) 
from ubd_tmp.pcp;



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值