LeetCode:Database 103.周内每天的销售情况

Posted Xiao Miao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode:Database 103.周内每天的销售情况相关的知识,希望对你有一定的参考价值。

要求:写一个SQL语句,报告 周内每天 每个商品类别下订购了多少单位,返回结果表单 按商品类别排序 。

你是企业主,想要获得分类商品和周内每天的销售报告。

表:Orders的结构

+---------------+---------+
| 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 的消费者订购的日期

表:Items的结构

+---------------------+---------+
| Column Name         | Type    |
+---------------------+---------+
| item_id             | varchar |
| item_name           | varchar |
| item_category       | varchar |
+---------------------+---------+
item_id 是该表主键
item_name 是商品的名字
item_category 是商品的类别

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 Table:

+------------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+
| 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 类别的商品

SQL语句:

with c as(
select a.order_id as o1,a.customer_id as c1,dayofweek(a.order_date) as d1,a.item_id as i1,a.quantity as q1,b.item_category as ic1 
from orders a
right join items b
on a.item_id=b.item_id)

select ic1 as Category,
sum(case when d1=2 then q1 else 0 end) as Monday,
sum(case when d1=3 then q1 else 0 end) as Tuesday,
sum(case when d1=4 then q1 else 0 end) as Wednesday,
sum(case when d1=5 then q1 else 0 end) as Thursday,
sum(case when d1=6 then q1 else 0 end) as Friday,
sum(case when d1=7 then q1 else 0 end) as Saturday,
sum(case when d1=1 then q1 else 0 end) as Sunday
from c
group by ic1
order by Category;

以上是关于LeetCode:Database 103.周内每天的销售情况的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode(数据库)- 周内每天的销售情况

使用oracle,如何做到某个时间段内每间隔1分钟查询出一条记录?

Leetcode 103

LeetCode ???103??? ?????????????????????????????????

leetcode103

103. leetcode笔记(1~60)