查询计算每一天的累计销售金额以及与前一天相比的销售金额增长额

  • 请编写一个Hive SQL查询,对于每个店铺,计算每一天的累计销售金额以及与前一天相比的销售金额增长额(如果是第一天,则增长额为0)。结果表应包含shop_id,sale_date,sale_amount,cumulative_sale_amount和sale_amount_increase这几列
    建表语句
CREATE TABLE sales_data (
  shop_id STRING,
  sale_date STRING,
  sale_amount DOUBLE
)
STORED AS PARQUET;  -- 可以选择不同的存储格式,如 Parquet, ORC, etc.
INSERT INTO sales_data VALUES
  ('shop_1', '2023-01-01', 100.0),
  ('shop_1', '2023-01-02', 150.0),
  ('shop_1', '2023-01-03', 200.0),
  ('shop_2', '2023-01-01', 80.0),
  ('shop_2', '2023-01-02', 120.0),
  ('shop_2', '2023-01-03', 160.0);

代码

select 
shop_id,
sale_date,
sale_amount,
sum(sale_amount) over(partition by shop_id order by sale_date) as cumulative_sale_amount,
if(lag(sale_amount) over(partition by shop_id order by sale_amount) is null , 0,
   sale_amount - lag(sale_amount) over(partition by shop_id order by sale_amount)
) as sale_amount_increase
from sales_data

结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值