9. Using SQL to Uncover the Truth: A Case StudyÂ
Activity 9.01: Quantifying the Sales Drop
Solution:
Perform the following steps to complete this activity:
- Load the
sqlda
database with psql. - Using the
OVER
andORDER BY
statements, compute the daily cumulative sum of sales. This provides you with a discrete count of sales over a period of time on a daily basis. Insert the results into a new table calledbat_sales_growth
:SELECT Â Â *, Â Â sum(count) OVER (ORDER BY sales_date) INTO Â Â bat_sales_growth FROM Â Â bat_sales_daily;
- Compute a seven-day
lag
of thesum
column, and then insert all the columns ofbat_sales_daily
and the newlag
column into a new table,bat_sales_daily_delay
. Thislag
column indicates the sales amount a week prior to the given record, allowing you to compare sales with the previous week:SELECT Â Â *, Â Â lag(sum, 7) OVER (ORDER BY sales_date) INTO Â Â bat_sales_daily_delay...