Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update total-sales-amount-by-year.sql #120

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 75 additions & 46 deletions MySQL/total-sales-amount-by-year.sql
Original file line number Diff line number Diff line change
@@ -1,38 +1,4 @@
# Time: O(nlogn)
# Space: O(n)

SELECT product_id,
product_name,
report_year,
(DATEDIFF(
CASE WHEN YEAR(period_end) > report_year THEN CONCAT(report_year, '-12-31') ELSE period_end END,
CASE WHEN YEAR(period_start) < report_year THEN CONCAT(report_year, '-01-01') ELSE period_start END
) + 1) * average_daily_sales AS total_amount
FROM (SELECT s.product_id,
product_name,
period_start,
period_end,
average_daily_sales
FROM sales s
INNER JOIN product p
ON s.product_id = p.product_id
) AS r,
(SELECT "2018" AS report_year
UNION ALL
SELECT "2019"
UNION ALL
SELECT "2020"
) AS y
WHERE YEAR(period_start) <= report_year AND
YEAR(period_end) >= report_year
GROUP BY product_id,
report_year
ORDER BY product_id,
report_year;


# Time: O(nlogn)
# Space: O(n)
-- hard coded
SELECT r.product_id,
product_name,
report_year,
Expand All @@ -43,10 +9,14 @@ FROM ((SELECT product_id,
FROM (SELECT product_id,
average_daily_sales,
DATEDIFF(
CASE WHEN period_end > '2018-12-31' THEN '2018-12-31' ELSE period_end END,
CASE WHEN period_start < '2018-01-01' THEN '2018-01-01' ELSE period_start END
DAY, -- added
period_start,
CASE WHEN period_end > '2018-12-31' THEN '2018-12-31' ELSE period_end END
--CASE WHEN period_start < '2018-01-01' THEN '2018-01-01' ELSE period_start END
) + 1 AS days
FROM sales s) tmp
FROM #Sales s
WHERE DATEPART(YEAR, period_start) = 2018 -- added
) tmp
WHERE days > 0)
UNION ALL
(SELECT product_id,
Expand All @@ -55,10 +25,14 @@ FROM ((SELECT product_id,
FROM (SELECT product_id,
average_daily_sales,
DATEDIFF(
CASE WHEN period_end > '2019-12-31' THEN '2019-12-31' ELSE period_end END,
CASE WHEN period_start < '2019-01-01' THEN '2019-01-01' ELSE period_start END
) + 1 AS days
FROM sales s) tmp
DAY, -- added
period_start,
CASE WHEN period_end > '2019-12-31' THEN '2019-12-31' ELSE period_end END
-- CASE WHEN period_start < '2019-01-01' THEN '2019-01-01' ELSE period_start END
) + 1 AS days
FROM #Sales s
WHERE DATEPART(YEAR, period_start) = 2019 -- added
) tmp
WHERE days > 0)
UNION ALL
(SELECT product_id,
Expand All @@ -67,13 +41,68 @@ FROM ((SELECT product_id,
FROM (SELECT product_id,
average_daily_sales,
DATEDIFF(
CASE WHEN period_end > '2020-12-31' THEN '2020-12-31' ELSE period_end END,
CASE WHEN period_start < '2020-01-01' THEN '2020-01-01' ELSE period_start END
DAY, -- added
period_start,-- added
CASE WHEN period_end > '2020-12-31' THEN '2020-12-31' ELSE period_end END
-- CASE WHEN period_start < '2020-01-01' THEN '2020-01-01' ELSE period_start END
) + 1 AS days
FROM #Sales s
WHERE DATEPART(YEAR, period_start) = 2020 -- added
) tmp
WHERE days > 0)
-- added this additional UNION
UNION ALL
(SELECT product_id,
'2019' AS report_year,
days * average_daily_sales AS total_amount
FROM (SELECT product_id,
average_daily_sales,
DATEDIFF(
DAY, -- added
'2019-01-01',-- added -- start_date
CASE WHEN period_end > '2018-12-31' AND period_end > '2019-12-31' THEN '2019-12-31' ELSE period_end END
) + 1 AS days
FROM #Sales s
WHERE DATEPART(YEAR, period_start) = 2018 -- added
AND DATEPART(YEAR, period_end) = 2020
) tmp
WHERE days > 0)
-- added this additional UNION
UNION ALL
(SELECT product_id,
'2020' AS report_year,
days * average_daily_sales AS total_amount
FROM (SELECT product_id,
average_daily_sales,
DATEDIFF(
DAY, -- added
'2020-01-01',-- added
period_end -- added
) + 1 AS days
FROM #Sales s
WHERE DATEPART(YEAR, period_start) = 2019 -- added
AND DATEPART(YEAR, period_end) = 2020
) tmp
WHERE days > 0)
-- added this additional UNION
UNION ALL
(SELECT product_id,
'2020' AS report_year,
days * average_daily_sales AS total_amount
FROM (SELECT product_id,
average_daily_sales,
DATEDIFF(
DAY, -- added
'2020-01-01',-- added
period_end -- added
) + 1 AS days
FROM sales s) tmp
FROM #Sales s
WHERE DATEPART(YEAR, period_start) = 2018 -- added
AND DATEPART(YEAR, period_end) = 2020 -- added
) tmp
WHERE days > 0)
) r
INNER JOIN product p
INNER JOIN #Product p
ON r.product_id = p.product_id
ORDER BY r.product_id,
report_year ;