|
| 1 | +Problem: 1083. Sales Analysis II |
| 2 | +Table: Orders |
| 3 | + |
| 4 | ++---------------+---------+ |
| 5 | +| Column Name | Type | |
| 6 | ++---------------+---------+ |
| 7 | +| order_id | int | |
| 8 | +| customer_id | int | |
| 9 | +| order_date | date | |
| 10 | +| item_id | varchar | |
| 11 | +| quantity | int | |
| 12 | ++---------------+---------+ |
| 13 | +(ordered_id, item_id) is the primary key for this table. |
| 14 | +This table contains information of the orders placed. |
| 15 | +order_date is the date when item_id was ordered by the customer with id customer_id. |
| 16 | + |
| 17 | + |
| 18 | +Table: Items |
| 19 | + |
| 20 | ++---------------------+---------+ |
| 21 | +| Column Name | Type | |
| 22 | ++---------------------+---------+ |
| 23 | +| item_id | varchar | |
| 24 | +| item_name | varchar | |
| 25 | +| item_category | varchar | |
| 26 | ++---------------------+---------+ |
| 27 | +item_id is the primary key for this table. |
| 28 | +item_name is the name of the item. |
| 29 | +item_category is the category of the item. |
| 30 | + |
| 31 | + |
| 32 | +You are the business owner and would like to obtain a sales report for category items and day of the week. |
| 33 | + |
| 34 | +Write an SQL query to report how many units in each category have been ordered on each day of the week. |
| 35 | + |
| 36 | +Return the result table ordered by category. |
| 37 | + |
| 38 | +The query result format is in the following example: |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +Orders table: |
| 43 | ++------------+--------------+-------------+--------------+-------------+ |
| 44 | +| order_id | customer_id | order_date | item_id | quantity | |
| 45 | ++------------+--------------+-------------+--------------+-------------+ |
| 46 | +| 1 | 1 | 2020-06-01 | 1 | 10 | |
| 47 | +| 2 | 1 | 2020-06-08 | 2 | 10 | |
| 48 | +| 3 | 2 | 2020-06-02 | 1 | 5 | |
| 49 | +| 4 | 3 | 2020-06-03 | 3 | 5 | |
| 50 | +| 5 | 4 | 2020-06-04 | 4 | 1 | |
| 51 | +| 6 | 4 | 2020-06-05 | 5 | 5 | |
| 52 | +| 7 | 5 | 2020-06-05 | 1 | 10 | |
| 53 | +| 8 | 5 | 2020-06-14 | 4 | 5 | |
| 54 | +| 9 | 5 | 2020-06-21 | 3 | 5 | |
| 55 | ++------------+--------------+-------------+--------------+-------------+ |
| 56 | + |
| 57 | +Items table: |
| 58 | ++------------+----------------+---------------+ |
| 59 | +| item_id | item_name | item_category | |
| 60 | ++------------+----------------+---------------+ |
| 61 | +| 1 | LC Alg. Book | Book | |
| 62 | +| 2 | LC DB. Book | Book | |
| 63 | +| 3 | LC SmarthPhone | Phone | |
| 64 | +| 4 | LC Phone 2020 | Phone | |
| 65 | +| 5 | LC SmartGlass | Glasses | |
| 66 | +| 6 | LC T-Shirt XL | T-Shirt | |
| 67 | ++------------+----------------+---------------+ |
| 68 | + |
| 69 | +Result table: |
| 70 | ++------------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+ |
| 71 | +| Category | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday | |
| 72 | ++------------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+ |
| 73 | +| Book | 20 | 5 | 0 | 0 | 10 | 0 | 0 | |
| 74 | +| Glasses | 0 | 0 | 0 | 0 | 5 | 0 | 0 | |
| 75 | +| Phone | 0 | 0 | 5 | 1 | 0 | 0 | 10 | |
| 76 | +| T-Shirt | 0 | 0 | 0 | 0 | 0 | 0 | 0 | |
| 77 | ++------------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+ |
| 78 | +On Monday (2020-06-01, 2020-06-08) were sold a total of 20 units (10 + 10) in the category Book (ids: 1, 2). |
| 79 | +On Tuesday (2020-06-02) were sold a total of 5 units in the category Book (ids: 1, 2). |
| 80 | +On Wednesday (2020-06-03) were sold a total of 5 units in the category Phone (ids: 3, 4). |
| 81 | +On Thursday (2020-06-04) were sold a total of 1 unit in the category Phone (ids: 3, 4). |
| 82 | +On Friday (2020-06-05) were sold 10 units in the category Book (ids: 1, 2) and 5 units in Glasses (ids: 5). |
| 83 | +On Saturday there are no items sold. |
| 84 | +On Sunday (2020-06-14, 2020-06-21) were sold a total of 10 units (5 +5) in the category Phone (ids: 3, 4). |
| 85 | +There are no sales of T-Shirt. |
| 86 | + |
| 87 | +-------------------------------------------------------------------------------------------------------------------------------------- |
| 88 | +Solution 1: |
| 89 | + |
| 90 | +SELECT i.item_category AS CATEGORY, |
| 91 | +SUM(CASE WHEN DAYOFWEEK(o.order_date) = 2 THEN o.quantity ELSE 0 END) AS MONDAY, |
| 92 | +SUM(CASE WHEN DAYOFWEEK(o.order_date) = 3 THEN o.quantity ELSE 0 END) AS TUESDAY, |
| 93 | +SUM(CASE WHEN DAYOFWEEK(o.order_date) = 4 THEN o.quantity ELSE 0 END) AS WEDNESDAY, |
| 94 | +SUM(CASE WHEN DAYOFWEEK(o.order_date) = 5 THEN o.quantity ELSE 0 END) AS THURSDAY, |
| 95 | +SUM(CASE WHEN DAYOFWEEK(o.order_date) = 6 THEN o.quantity ELSE 0 END) AS FRIDAY, |
| 96 | +SUM(CASE WHEN DAYOFWEEK(o.order_date) = 7 THEN o.quantity ELSE 0 END) AS SATURDAY, |
| 97 | +SUM(CASE WHEN DAYOFWEEK(o.order_date) = 1 THEN o.quantity ELSE 0 END) AS SUNDAY |
| 98 | + |
| 99 | +FROM Items AS i |
| 100 | +LEFT JOIN Orders AS o |
| 101 | +ON i.item_id = o.item_id |
| 102 | +GROUP BY CATEGORY |
| 103 | +ORDER BY CATEGORY; |
| 104 | + |
| 105 | +-------------------------------------------------------------------------------------------------------------------------------------- |
0 commit comments