We are going to start off with sql query practice. Start postgres by searching it with spotlight (its started when you can see the psql elephant at the top right of your screen). Then run
psqlNow, inside the psql shell:
CREATE DATABASE sales(This database should already exist if you created it last week for Eric's review). Then run:
psql sales < sales.sqlSuper quick review of what we just did. First, you got postgres started by searching it with spotlight (or if you run linux or windows I have no idea what you did but hopefully you are still with me). Second we started psql and created the database called sales. Third we used the command line to ask psql to run the sales.sql file inside the sales database. This executes all the sql commands in the file inside the sales database. Open up the file to see what it does. It drops the sales table in case it already exists so we can overwrite it, then it creates it again. Finally it inserts some data.
So we have a sales database, and a sales table inside that database. We also have a file called sales.sql that creates the table and inserts data into it.
Now, start psql again inside the sales database:
psql salesand try answer these questions.
I'll do my best to explain window functions. I learned it from here so you can follow along.
- Write a cumulative sum with a window function. (You should have an OVER clause)
- By month, which product type sold more. (You should use the RANK() function.)
- Create a monthly running total by product type.
- Use CASE to sum up only Jan and Feb sales by type.
Answers are in the sql_practice.sql file.
Pop open the ipython notebook, we are going to talk about writing code using documentation you know nothing about.