Skip to content

Commit 3ffa368

Browse files
authored
Create 1384-total-sales-amount-by-year.sql
1 parent 158283d commit 3ffa368

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

1384-total-sales-amount-by-year.sql

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Write your MySQL query statement below
2+
SELECT
3+
b.product_id,
4+
a.product_name,
5+
a.yr AS report_year,
6+
CASE
7+
WHEN YEAR(b.period_start)=YEAR(b.period_end) AND a.yr=YEAR(b.period_start) THEN DATEDIFF(b.period_end,b.period_start)+1
8+
WHEN a.yr=YEAR(b.period_start) THEN DATEDIFF(DATE_FORMAT(b.period_start,'%Y-12-31'),b.period_start)+1
9+
WHEN a.yr=YEAR(b.period_end) THEN DAYOFYEAR(b.period_end)
10+
WHEN a.yr>YEAR(b.period_start) AND a.yr<YEAR(b.period_end) THEN 365
11+
ELSE 0
12+
END * average_daily_sales AS total_amount
13+
FROM
14+
(SELECT product_id,product_name,'2018' AS yr FROM Product
15+
UNION
16+
SELECT product_id,product_name,'2019' AS yr FROM Product
17+
UNION
18+
SELECT product_id,product_name,'2020' AS yr FROM Product) a
19+
JOIN
20+
Sales b
21+
ON a.product_id=b.product_id
22+
HAVING total_amount > 0
23+
ORDER BY b.product_id,a.yr;

0 commit comments

Comments
 (0)