Skip to content

Commit 4d8ff90

Browse files
refactor 1384
1 parent 084b3b1 commit 4d8ff90

File tree

1 file changed

+0
-69
lines changed

1 file changed

+0
-69
lines changed

database/_1384.sql

-69
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,3 @@
1-
--1384. Total Sales Amount by Year
2-
--
3-
--Table: Product
4-
--
5-
--+---------------+---------+
6-
--| Column Name | Type |
7-
--+---------------+---------+
8-
--| product_id | int |
9-
--| product_name | varchar |
10-
--+---------------+---------+
11-
--product_id is the primary key for this table.
12-
--product_name is the name of the product.
13-
--
14-
--
15-
--Table: Sales
16-
--
17-
--+---------------------+---------+
18-
--| Column Name | Type |
19-
--+---------------------+---------+
20-
--| product_id | int |
21-
--| period_start | varchar |
22-
--| period_end | date |
23-
--| average_daily_sales | int |
24-
--+---------------------+---------+
25-
--product_id is the primary key for this table.
26-
--period_start and period_end indicates the start and end date for sales period, both dates are inclusive.
27-
--The average_daily_sales column holds the average daily sales amount of the items for the period.
28-
--
29-
--Write an SQL query to report the Total sales amount of each item for each year, with corresponding product name, product_id, product_name and report_year.
30-
--
31-
--Dates of the sales years are between 2018 to 2020. Return the result table ordered by product_id and report_year.
32-
--
33-
--The query result format is in the following example:
34-
--
35-
--
36-
--Product table:
37-
--+------------+--------------+
38-
--| product_id | product_name |
39-
--+------------+--------------+
40-
--| 1 | LC Phone |
41-
--| 2 | LC T-Shirt |
42-
--| 3 | LC Keychain |
43-
--+------------+--------------+
44-
--
45-
--Sales table:
46-
--+------------+--------------+-------------+---------------------+
47-
--| product_id | period_start | period_end | average_daily_sales |
48-
--+------------+--------------+-------------+---------------------+
49-
--| 1 | 2019-01-25 | 2019-02-28 | 100 |
50-
--| 2 | 2018-12-01 | 2020-01-01 | 10 |
51-
--| 3 | 2019-12-01 | 2020-01-31 | 1 |
52-
--+------------+--------------+-------------+---------------------+
53-
--
54-
--Result table:
55-
--+------------+--------------+-------------+--------------+
56-
--| product_id | product_name | report_year | total_amount |
57-
--+------------+--------------+-------------+--------------+
58-
--| 1 | LC Phone | 2019 | 3500 |
59-
--| 2 | LC T-Shirt | 2018 | 310 |
60-
--| 2 | LC T-Shirt | 2019 | 3650 |
61-
--| 2 | LC T-Shirt | 2020 | 10 |
62-
--| 3 | LC Keychain | 2019 | 31 |
63-
--| 3 | LC Keychain | 2020 | 31 |
64-
--+------------+--------------+-------------+--------------+
65-
--LC Phone was sold for the period of 2019-01-25 to 2019-02-28, and there are 35 days for this period. Total amount 35*100 = 3500.
66-
--LC T-shirt was sold for the period of 2018-12-01 to 2020-01-01, and there are 31, 365, 1 days for years 2018, 2019 and 2020 respectively.
67-
--LC Keychain was sold for the period of 2019-12-01 to 2020-01-31, and there are 31, 31 days for years 2019 and 2020 respectively.
68-
--
69-
701
/* Write your T-SQL query statement below */
712
--credit: https://leetcode.com/problems/total-sales-amount-by-year/discuss/544812/Using-Recursive-CTE-to-get-all-the-possible-dates
723

0 commit comments

Comments
 (0)