今日真题
题目介绍: 周内每天的销售情况 sales-by-day-of-the-week
难度困难
SQL架构
表:
代码语言:javascript复制Orders
代码语言:javascript复制 --------------- ---------
| Column Name | Type |
--------------- ---------
| order_id | int |
| customer_id | int |
| order_date | date |
| item_id | varchar |
| quantity | int |
--------------- ---------
(order_id, item_id) 是该表主键
该表包含了订单信息
order_date 是id为 item_id 的商品被id为 customer_id 的消费者订购的日期.
表:
代码语言:javascript复制Items
代码语言:javascript复制 --------------------- ---------
| Column Name | Type |
--------------------- ---------
| item_id | varchar |
| item_name | varchar |
| item_category | varchar |
--------------------- ---------
item_id 是该表主键
item_name 是商品的名字
item_category 是商品的类别
你是企业主,想要获得分类商品和周内每天的销售报告。
写一个SQL语句,报告 周内每天 每个商品类别下订购了多少单位。
返回结果表单 按商品类别排序 。
查询结果格式如下例所示:
``` Orders 表: ------------ -------------- ------------- -------------- ------------- | order_id | customer_id | order_date | item_id | quantity | ------------ -------------- ------------- -------------- ------------- | 1 | 1 | 2020-06-01 | 1 | 10 | | 2 | 1 | 2020-06-08 | 2 | 10 | | 3 | 2 | 2020-06-02 | 1 | 5 | | 4 | 3 | 2020-06-03 | 3 | 5 | | 5 | 4 | 2020-06-04 | 4 | 1 | | 6 | 4 | 2020-06-05 | 5 | 5 | | 7 | 5 | 2020-06-05 | 1 | 10 | | 8 | 5 | 2020-06-14 | 4 | 5 | | 9 | 5 | 2020-06-21 | 3 | 5 | ------------ -------------- ------------- -------------- -------------
Items 表: ------------ ---------------- --------------- | item_id | item_name | item_category | ------------ ---------------- --------------- | 1 | LC Alg. Book | Book | | 2 | LC DB. Book | Book | | 3 | LC SmarthPhone | Phone | | 4 | LC Phone 2020 | Phone | | 5 | LC SmartGlass | Glasses | | 6 | LC T-Shirt XL | T-Shirt | ------------ ---------------- ---------------
Result 表: ------------ ----------- ----------- ----------- ----------- ----------- ----------- ----------- | Category | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday | ------------ ----------- ----------- ----------- ----------- ----------- ----------- ----------- | Book | 20 | 5 | 0 | 0 | 10 | 0 | 0 | | Glasses | 0 | 0 | 0 | 0 | 5 | 0 | 0 | | Phone | 0 | 0 | 5 | 1 | 0 | 0 | 10 | | T-Shirt | 0 | 0 | 0 | 0 | 0 | 0 | 0 | ------------ ----------- ----------- ----------- ----------- ----------- ----------- ----------- 在周一(2020-06-01, 2020-06-08),Book分类(ids: 1, 2)下,总共销售了20个单位(10 10) 在周二(2020-06-02),Book分类(ids: 1, 2)下,总共销售了5个单位 在周三(2020-06-03),Phone分类(ids: 3, 4)下,总共销售了5个单位 在周四(2020-06-04),Phone分类(ids: 3, 4)下,总共销售了1个单位 在周五(2020-06-05),Book分类(ids: 1, 2)下,总共销售了10个单位,Glasses分类(ids: 5)下,总共销售了5个单位 在周六, 没有商品销售 在周天(2020-06-14, 2020-06-21),Phone分类(ids: 3, 4)下,总共销售了10个单位(5 5) 没有销售 T-Shirt 类别的商品 ```
代码语言:javascript复制sql
select item_category as category,
sum(case when num = 2 then quantity else 0 end) as Monday,
sum(case when num = 3 then quantity else 0 end) as Tuesday,
sum(case when num = 4 then quantity else 0 end) as Wednesday,
sum(case when num = 5 then quantity else 0 end) as Thursday,
sum(case when num = 6 then quantity else 0 end) as Friday,
sum(case when num = 7 then quantity else 0 end) as Saturday,
sum(case when num = 1 then quantity else 0 end) as Sunday
from
(select item_category, quantity,dayofweek(order_date) as num from
items i left join orders o
on i.item_id=o.item_id) t
group by item_category
order by item_category
- 已经有灵感了?在评论区写下你的思路吧!