Answered step by step
Verified Expert Solution
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
Find holiday days, and days within a one week of a holiday, where the
actual sales are reduced by 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 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 query is given below which used all three subqueries. I get WRONGINCORRECT expectedsalesdollars 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 expectedsalesdollars. All subqueries work as expected individually. Subquery a gives rows when run by itself. Subquery b gives rows when run by itself. Subquery c gives rows when run by itself.
My query after using all the sub queries:
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
saledate,
actualsalesdollars
FROM
salesdata
UNION
SELECT
saledate,
salesdollars AS actualsalesdollars
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;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started