Search icon
Subscription
0
Cart icon
Close icon
You have no products in your basket yet
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
The Applied SQL Data Analytics Workshop - Second Edition

You're reading from  The Applied SQL Data Analytics Workshop - Second Edition

Product type Book
Published in Feb 2020
Publisher Packt
ISBN-13 9781800203679
Pages 484 pages
Edition 2nd Edition
Languages
Authors (3):
Matt Goldwasser Matt Goldwasser
Profile icon Matt Goldwasser
Upom Malik Upom Malik
Profile icon Upom Malik
Benjamin Johnston Benjamin Johnston
Profile icon Benjamin Johnston
View More author details

7. The Scientific Method and Applied Problem Solving

Activity 7.01: Quantifying the Sales Drop

Solution

  1. Load the sqlda database:
    $ psql sqlda
  2. Compute the daily cumulative sum of sales using the OVER and ORDER BY statements. Insert the results into a new table called bat_sales_growth:
    sqlda=# SELECT *, sum(count) OVER (ORDER BY sales_transaction_date) INTO bat_sales_growth FROM bat_sales_daily;

    The following output should be produced:

    SELECT 964

    Compute a 7-day lag function of the sum column and insert all the columns of bat_sales_daily and the new lag column into a new table, called bat_sales_daily_delay. This lag column indicates what the sales were 1 week before the given record:

    sqlda=# SELECT *, lag(sum, 7) OVER (ORDER BY sales_transaction_date) INTO bat_sales_daily_delay FROM bat_sales_growth;
  3. Inspect the first 15 rows of bat_sales_growth:
    sqlda=# SELECT * FROM bat_sales_daily_delay LIMIT 15;

    The following is the output of the preceding code:

    Figure 7.27: Daily sales...

lock icon The rest of the chapter is locked
arrow left Previous Chapter
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}