Hive 刷题——销售额完成任务指标的商品

需求描述

商家要求每个商品每个月需要售卖出一定的销售总额假设1号商品销售总额大于21000,2号商品销售总额大于10000,其余商品没有要求请写出SQL从订单详情表中(order_detail)查询连续两个月销售总额大于等于任务总额的商品
结果如下:
sku_id
 
(商品id)
1
需要用到的表:
订单明细表:order_detail
参考
order_id(订单id)
sku_id(商品id)
create_date(下单日期)
price(商品单价)
sku_num(商品件数)
1
1
1
2021-09-30
2000.00
2
2
1
3
2021-09-30
5000.00
5
22
10
4
2020-10-02
6000.00
1
23
10
5
2020-10-02
500.00
24
24
10
6
2020-10-02
2000.00
5

参考SQL

select sku_id
from (select t.sku_id,
             create_month,
             total_amaunt,
             flag_max,
             lag(total_amaunt, 1) over (partition by t.sku_id order by create_month) next_month
      from (select sku_id, month(create_date) create_month, sum(sku_num * price) total_amaunt
            from order_detail
            group by sku_id, month(create_date)) t
               join
               (select '1' sku_id, 21000 flag_max union all select '2' sku_id, 10000) t1 on t.sku_id = t1.sku_id) i
where next_month > flag_max
  and total_amaunt > flag_max;
posted @ 2023-02-02 09:41  晓枫的春天  阅读(45)  评论(0编辑  收藏  举报