Skip to content

Commit 1b88c99

Browse files
add 1384
1 parent 7aa0d60 commit 1b88c99

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,7 @@ _If you like this project, please leave me a star._ ★
929929

930930
| # | Title | Solutions | Video | Difficulty | Tag
931931
|-----|----------------|---------------|---------------|---------------|-------------
932+
|1384|[Total Sales Amount by Year](https://leetcode.com/problems/total-sales-amount-by-year/)|[Solution](../master/database/_1384.sql) || Hard |
932933
|1378|[Replace Employee ID With The Unique Identifier](https://leetcode.com/problems/replace-employee-id-with-the-unique-identifier/)|[Solution](../master/database/_1378.sql) || Easy |
933934
|1369|[Get the Second Most Recent Activity](https://leetcode.com/problems/get-the-second-most-recent-activity/)|[Solution](../master/database/_1369.sql) || Hard |
934935
|1364|[Number of Trusted Contacts of a Customer](https://leetcode.com/problems/number-of-trusted-contacts-of-a-customer/)|[Solution](../master/database/_1364.sql) || Medium |

database/_1384.sql

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
70+
/* Write your T-SQL query statement below */
71+
--credit: https://leetcode.com/problems/total-sales-amount-by-year/discuss/544812/Using-Recursive-CTE-to-get-all-the-possible-dates
72+
73+
with dates as
74+
(select s_date = min(period_start), e_date = max(period_end) from sales
75+
union all
76+
select dateadd(day, 1 ,s_date) , e_date from dates
77+
where s_date<e_date
78+
)
79+
select
80+
PRODUCT_ID = cast(p.product_id as varchar(200))
81+
,PRODUCT_NAME = p.product_name
82+
,REPORT_YEAR = cast(year(s_date) as varchar(10))
83+
,TOTAL_AMOUNT = sum(average_daily_sales)
84+
from product p
85+
left outer join sales s on p.product_id = s.product_id
86+
left outer join dates d on d.s_date between s.period_start and s.period_end
87+
group by p.product_id , p.product_name, year(s_date)
88+
order by 1,3
89+
option(maxrecursion 0)

0 commit comments

Comments
 (0)