32. 某周内每件商品每天销售情况

文章目录

        • 题目需求
        • 实现一
        • 题目来源

题目需求

从订单明细表(order_detail)中查询2021年9月27号-2021年10月3号这一周所有商品每天销售情况。

期望结果如下(截取部分):

sku_id monday tuesday wednesday thursday friday saturday sunday
1 0 0 9 8 0 4 2
10 0 0 0 0 48 69 0
11 0 0 0 0 15 61 0
12 0 0 43 0 31 20400 0
2 0 0 0 18 5800 0 0

需要用到的表:

订单明细表:order_detail

order_detail_id(订单明细id) 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

实现一

select sku_id,
       sum(if(create_date = '2021-09-27', sku_num, 0)) as monday,
       sum(if(create_date = '2021-09-28', sku_num, 0)) as tuesday,
       sum(if(create_date = '2021-09-29', sku_num, 0)) as wednesday,
       sum(if(create_date = '2021-09-30', sku_num, 0)) as thursday,
       sum(if(create_date = '2021-10-01', sku_num, 0)) as friday,
       sum(if(create_date = '2021-10-02', sku_num, 0)) as saturday,
       sum(if(create_date = '2021-10-03', sku_num, 0)) as sunday
from order_detail
where '2021-09-27' <= order_detail.create_date
  and order_detail.create_date <= '2021-10-03'
group by sku_id;

题目来源

http://practice.atguigu.cn/#/question/32/desc?qType=SQL

你可能感兴趣的:(#,HQL,hive,sql,数据库)