Answered step by step
Verified Expert Solution
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 returns the holidayname, dateanalyzed, dow, and dayofweek. The query in returns the dow, dayofweek, and expectedsalesdollars. The query in returns the saledate and actualsalesdollars for all days of even days where AGM was closed. Join the subqueries together to return holidayname, dateanalyzed, dayofweek, actualsalesdollars, and expectedsalesdollars. Derive a column ratioactualexpected by dividing actualsalesdollars by the expectedsalesdollars, rounded as shown. Filter where the ratioactualexpected unrounded is or less. Sort by dateanalyzed. My final query is: WITH a AS SELECT hdescription AS holidayname, mydate::date AS dateanalyzed, EXTRACTDOW FROM mydate::date AS dow, TOCHARmydate::date, 'Day' AS dayofweek FROM generateseries day'::interval AS mydate INNER JOIN holidays AS h ON mydate::date BETWEEN hholidaydate INTERVAL days' AND hholidaydate INTERVAL days' WHERE mydate::date BETWEEN AND b AS SELECT EXTRACTDOW FROM mydate::date AS dow, TOCHARmydate::date, 'Day' AS dayofweek, ROUNDAVGCOALESCEsatotalamount, AS expectedsalesdollars FROM generateseries day'::interval AS mydate INNER JOIN sales AS sa ON mydate::date sasaledate WHERE mydate::date BETWEEN AND GROUP BY dow, dayofweek c AS With salesdata as SELECT mydate::date AS saledate, COALESCESUMsatotalamount AS actualsalesdollars From generateseries day'::interval AS mydate Left Join sales as sa ON mydate::date sasaledate Where mydate::date BETWEEN AND Group By mydate::date closeddays as SELECT holidaydate AS saledate, AS salesdollars From holidays Where closedflag true AND holidaydate BETWEEN AND Select From salesdata UNION Select From closeddays SELECT aholidayname, adateanalyzed, adayofweek, cactualsalesdollars, bexpectedsalesdollars, ROUNDcactualsalesdollars bexpectedsalesdollars AS ratioactualexpected FROM a Inner Join c ON adateanalyzed csaledate Left Join b ON adow bdow AND adayofweek bdayofweek WHERE cactualsalesdollars bexpectedsalesdollars Order By adateanalyzed; The output I am getting is showing few rows, not all: holidayname dateanalyzed dayofweek actualsalesdollars expectedsalesdollars ratioactualexpected New Year's Day Wednesday New Year's Day Tuesday MLK Day Tuesday MLK Day Friday MLK Day Saturday MLK Day Sunday MLK Day Monday The output I should have is: holidayname dateanalyzed dayofweek actualsalesdollars expectedsalesdollars ratioactualexpected New Year's Day Wednesday MLK Day Friday MLK Day Saturday MLK Day Sunday MLK Day Monday President's Day Friday President's Day Saturday President's Day Sunday President's Day Monday Easter Sunday Mother's Day Sunday Memorial Day Friday Memorial Day Saturday Memorial Day Sunday Memorial Day Monday Father's Day Sunday Independence Day Friday Independence Day
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started
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 returns the holidayname, dateanalyzed, dow, and dayofweek.
The query in returns the dow, dayofweek, and expectedsalesdollars.
The query in returns the saledate and actualsalesdollars for all days of even days where AGM was closed.
Join the subqueries together to return holidayname, dateanalyzed, dayofweek, actualsalesdollars, and expectedsalesdollars.
Derive a column ratioactualexpected by dividing actualsalesdollars by the expectedsalesdollars, rounded as shown.
Filter where the ratioactualexpected unrounded is or less.
Sort by dateanalyzed.
My final query is:
WITH a AS
SELECT
hdescription AS holidayname,
mydate::date AS dateanalyzed,
EXTRACTDOW FROM mydate::date AS dow,
TOCHARmydate::date, 'Day' AS dayofweek
FROM
generateseries day'::interval AS mydate
INNER JOIN
holidays AS h
ON
mydate::date BETWEEN hholidaydate INTERVAL days' AND hholidaydate INTERVAL days'
WHERE
mydate::date BETWEEN AND
b AS
SELECT
EXTRACTDOW FROM mydate::date AS dow,
TOCHARmydate::date, 'Day' AS dayofweek,
ROUNDAVGCOALESCEsatotalamount, AS expectedsalesdollars
FROM
generateseries day'::interval AS mydate
INNER JOIN
sales AS sa
ON
mydate::date sasaledate
WHERE
mydate::date BETWEEN AND
GROUP BY
dow, dayofweek
c AS
With salesdata as
SELECT mydate::date AS saledate,
COALESCESUMsatotalamount AS actualsalesdollars
From
generateseries day'::interval AS mydate
Left Join
sales as sa
ON mydate::date sasaledate
Where mydate::date BETWEEN AND
Group By
mydate::date
closeddays as
SELECT holidaydate AS saledate,
AS salesdollars
From holidays
Where closedflag true
AND holidaydate BETWEEN AND
Select From salesdata
UNION
Select From closeddays
SELECT
aholidayname,
adateanalyzed,
adayofweek,
cactualsalesdollars,
bexpectedsalesdollars,
ROUNDcactualsalesdollars bexpectedsalesdollars AS ratioactualexpected
FROM a
Inner Join c
ON adateanalyzed csaledate
Left Join b
ON adow bdow AND adayofweek bdayofweek
WHERE cactualsalesdollars bexpectedsalesdollars
Order By
adateanalyzed;
The output I am getting is showing few rows, not all:
holidayname dateanalyzed dayofweek actualsalesdollars expectedsalesdollars ratioactualexpected
New Year's Day Wednesday
New Year's Day Tuesday
MLK Day Tuesday
MLK Day Friday
MLK Day Saturday
MLK Day Sunday
MLK Day Monday
The output I should have is:
holidayname dateanalyzed dayofweek actualsalesdollars expectedsalesdollars ratioactualexpected
New Year's Day Wednesday
MLK Day Friday
MLK Day Saturday
MLK Day Sunday
MLK Day Monday
President's Day Friday
President's Day Saturday
President's Day Sunday
President's Day Monday
Easter Sunday
Mother's Day Sunday
Memorial Day Friday
Memorial Day Saturday
Memorial Day Sunday
Memorial Day Monday
Father's Day Sunday
Independence Day Friday
Independence Day
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started