Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Now that we have some preliminary queries written, we can use them as subqueries to a larger query. Remember to remove the order by

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 final query is:
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 * From sales_data
UNION
Select * 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;
The output I am getting is (showing few rows, not all):
holiday_name date_analyzed day_of_week actual_sales_dollars expected_sales_dollars ratio_actual_expected
0 New Year's Day 2020-01-01 Wednesday 13377664.212083.41
1 New Year's Day 2020-01-07 Tuesday 13455664.152097.52
2 MLK Day 2020-01-14 Tuesday 13260064.152067.03
3 MLK Day 2020-01-17 Friday 12709264.061983.95
4 MLK Day 2020-01-18 Saturday 13520464.302102.71
5 MLK Day 2020-01-19 Sunday 13036864.132032.87
6 MLK Day 2020-01-20 Monday 13074064.262034.55
The output I should have is:
holiday_name date_analyzed day_of_week actual_sales_dollars expected_sales_dollars ratio_actual_expected
0 New Year's Day 2020-01-01 Wednesday 1337762632560.51
1 MLK Day 2020-01-17 Friday 1270922525220.5
2 MLK Day 2020-01-18 Saturday 1352043734900.36
3 MLK Day 2020-01-19 Sunday 1303683574820.36
4 MLK Day 2020-01-20 Monday 1307402532250.52
5 President's Day 2020-02-14 Friday 1334522525220.53
6 President's Day 2020-02-15 Saturday 1320963734900.35
7 President's Day 2020-02-16 Sunday 1321803574820.37
8 President's Day 2020-02-17 Monday 1352282532250.53
9 Easter 2020-04-12 Sunday 1361643574820.38
10 Mother's Day 2020-05-10 Sunday 1344963574820.38
11 Memorial Day 2020-05-22 Friday 1291322525220.51
12 Memorial Day 2020-05-23 Saturday 1329003734900.36
13 Memorial Day 2020-05-24 Sunday 1307643574820.37
14 Memorial Day 2020-05-25 Monday 1326242532250.52
15 Father's Day 2020-06-21 Sunday 1351203574820.38
16 Independence Day 2020-07-03 Friday 1329602525220.53
17 Independence Day

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions