Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1 . 4 . 6 Find holiday days, and days within a one week of a holiday, where the actual sales are reduced by 1

1.4.6 Find holiday days, and days within a one week of a holiday, where the
actual sales are reduced by 15% or more from expected sales
Now that we have some preliminary queries written, we can use them as subqueries to a larger query. Remember to remove the "order by" clause when using
them as subqueries.
The query in 1.4.2 returns the holiday_name, date_analyzed, dow, and day_of_week.
The query in 1.4.4 returns the dow, day_of_week, and expected_sales_dollars.
The query in 1.4.5 returns the sale_date and actual_sales_dollars for all days of 2020, even days where AGM was closed.
Join the 3 subqueries together to return holiday_name, date_analyzed, day_of_week, actual_sales_dollars, and expected_sales_dollars.
Derive a column ratio_actual_expected by dividing actual_sales_dollars by the expected_sales_dollars, rounded as shown.
Filter where the ratio_actual_expected (unrounded) is 85% or less.
Sort by date_analyzed.
My query is given below which used all three subqueries. I get WRONG/INCORRECT expected_sales_dollars data in the rows. This causes the ratio to be incorrect as well. Please check the joins and the overall query to find why the query gives incorrect expected_sales_dollars. All subqueries work as expected individually. Subquery "a" gives 172 rows when run by itself. Subquery "b" gives 7 rows when run by itself. Subquery "c " gives 366 rows when run by itself.
My query after using all the sub queries:
WITH a AS (
SELECT
h.description AS holiday_name,
my_date::date AS date_analyzed,
EXTRACT(DOW FROM my_date::date) AS dow,
TO_CHAR(my_date::date, 'Day') AS day_of_week
FROM
generate_series('2020-01-01','2020-12-31','1 day'::interval) AS my_date
INNER JOIN holidays AS h ON my_date::date BETWEEN h.holiday_date - INTERVAL '7 days' AND h.holiday_date + INTERVAL '7 days'
WHERE
my_date::date BETWEEN '2020-01-01' AND '2020-12-31'
),
b AS (
SELECT
EXTRACT(DOW FROM my_date::date) AS dow,
TO_CHAR(my_date::date, 'Day') AS day_of_week,
ROUND(AVG(COALESCE(sa.total_amount, 0)),2) AS expected_sales_dollars
FROM
generate_series('2020-01-01','2020-12-31','1 day'::interval) AS my_date
INNER JOIN sales AS sa ON my_date::date = sa.sale_date
WHERE
my_date::date BETWEEN '2020-01-01' AND '2020-12-31'
GROUP BY
dow, day_of_week
),
c AS (
WITH sales_data AS (
SELECT
my_date::date AS sale_date,
COALESCE(SUM(sa.total_amount),0) AS actual_sales_dollars
FROM
generate_series('2020-01-01','2020-12-31','1 day'::interval) AS my_date
LEFT JOIN sales AS sa ON my_date::date = sa.sale_date
WHERE
my_date::date BETWEEN '2020-01-01' AND '2020-12-31'
GROUP BY
my_date::date
),
closed_days AS (
SELECT
holiday_date AS sale_date,
0 AS sales_dollars
FROM
holidays
WHERE
closed_flag = true AND holiday_date BETWEEN '2020-01-01' AND '2020-12-31'
)
SELECT
sale_date,
actual_sales_dollars
FROM
sales_data
UNION
SELECT
sale_date,
sales_dollars AS actual_sales_dollars
FROM
closed_days
)
SELECT
a.holiday_name,
a.date_analyzed,
a.day_of_week,
c.actual_sales_dollars,
b.expected_sales_dollars,
ROUND((c.actual_sales_dollars / b.expected_sales_dollars),2) AS ratio_actual_expected
FROM
a
INNER JOIN c ON a.date_analyzed = c.sale_date
LEFT JOIN b ON a.dow = b.dow AND a.day_of_week = b.day_of_week
WHERE
(c.actual_sales_dollars / b.expected_sales_dollars)<=0.85
ORDER BY
a.date_analyzed;

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M Kroenke, David J Auer

6th Edition

0132742926, 978-0132742924

More Books

Students also viewed these Databases questions

Question

x-3+1, x23 Let f(x) = -*+3, * Answered: 1 week ago

Answered: 1 week ago