|
@@ -2,6 +2,7 @@
|
|
|
"cells": [
|
|
|
{
|
|
|
"cell_type": "markdown",
|
|
|
+ "id": "0f858d38",
|
|
|
"metadata": {},
|
|
|
"source": [
|
|
|
"## Introduction and Schema Diagram"
|
|
@@ -10,27 +11,18 @@
|
|
|
{
|
|
|
"cell_type": "code",
|
|
|
"execution_count": 1,
|
|
|
+ "id": "30403e4a",
|
|
|
"metadata": {},
|
|
|
- "outputs": [
|
|
|
- {
|
|
|
- "data": {
|
|
|
- "text/plain": [
|
|
|
- "'Connected: None@chinook.db'"
|
|
|
- ]
|
|
|
- },
|
|
|
- "execution_count": 1,
|
|
|
- "metadata": {},
|
|
|
- "output_type": "execute_result"
|
|
|
- }
|
|
|
- ],
|
|
|
+ "outputs": [],
|
|
|
"source": [
|
|
|
"%%capture\n",
|
|
|
"%load_ext sql\n",
|
|
|
- "%sql sqlite:///chinook.db"
|
|
|
+ "%sql sqlite:///chinook.db\n"
|
|
|
]
|
|
|
},
|
|
|
{
|
|
|
"cell_type": "markdown",
|
|
|
+ "id": "2bd167b2",
|
|
|
"metadata": {},
|
|
|
"source": [
|
|
|
"## Overview of the Data"
|
|
@@ -39,12 +31,14 @@
|
|
|
{
|
|
|
"cell_type": "code",
|
|
|
"execution_count": 2,
|
|
|
+ "id": "637ac6c4",
|
|
|
"metadata": {},
|
|
|
"outputs": [
|
|
|
{
|
|
|
"name": "stdout",
|
|
|
"output_type": "stream",
|
|
|
"text": [
|
|
|
+ " * sqlite:///chinook.db\n",
|
|
|
"Done.\n"
|
|
|
]
|
|
|
},
|
|
@@ -52,54 +46,58 @@
|
|
|
"data": {
|
|
|
"text/html": [
|
|
|
"<table>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <th>name</th>\n",
|
|
|
- " <th>type</th>\n",
|
|
|
- " </tr>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <td>album</td>\n",
|
|
|
- " <td>table</td>\n",
|
|
|
- " </tr>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <td>artist</td>\n",
|
|
|
- " <td>table</td>\n",
|
|
|
- " </tr>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <td>customer</td>\n",
|
|
|
- " <td>table</td>\n",
|
|
|
- " </tr>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <td>employee</td>\n",
|
|
|
- " <td>table</td>\n",
|
|
|
- " </tr>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <td>genre</td>\n",
|
|
|
- " <td>table</td>\n",
|
|
|
- " </tr>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <td>invoice</td>\n",
|
|
|
- " <td>table</td>\n",
|
|
|
- " </tr>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <td>invoice_line</td>\n",
|
|
|
- " <td>table</td>\n",
|
|
|
- " </tr>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <td>media_type</td>\n",
|
|
|
- " <td>table</td>\n",
|
|
|
- " </tr>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <td>playlist</td>\n",
|
|
|
- " <td>table</td>\n",
|
|
|
- " </tr>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <td>playlist_track</td>\n",
|
|
|
- " <td>table</td>\n",
|
|
|
- " </tr>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <td>track</td>\n",
|
|
|
- " <td>table</td>\n",
|
|
|
- " </tr>\n",
|
|
|
+ " <thead>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <th>name</th>\n",
|
|
|
+ " <th>type</th>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " </thead>\n",
|
|
|
+ " <tbody>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <td>album</td>\n",
|
|
|
+ " <td>table</td>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <td>artist</td>\n",
|
|
|
+ " <td>table</td>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <td>customer</td>\n",
|
|
|
+ " <td>table</td>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <td>employee</td>\n",
|
|
|
+ " <td>table</td>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <td>genre</td>\n",
|
|
|
+ " <td>table</td>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <td>invoice</td>\n",
|
|
|
+ " <td>table</td>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <td>invoice_line</td>\n",
|
|
|
+ " <td>table</td>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <td>media_type</td>\n",
|
|
|
+ " <td>table</td>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <td>playlist</td>\n",
|
|
|
+ " <td>table</td>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <td>playlist_track</td>\n",
|
|
|
+ " <td>table</td>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <td>track</td>\n",
|
|
|
+ " <td>table</td>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " </tbody>\n",
|
|
|
"</table>"
|
|
|
],
|
|
|
"text/plain": [
|
|
@@ -132,6 +130,7 @@
|
|
|
},
|
|
|
{
|
|
|
"cell_type": "markdown",
|
|
|
+ "id": "13d5ed1b",
|
|
|
"metadata": {},
|
|
|
"source": [
|
|
|
"## Selecting New Albums to Purchase"
|
|
@@ -140,12 +139,14 @@
|
|
|
{
|
|
|
"cell_type": "code",
|
|
|
"execution_count": 3,
|
|
|
+ "id": "c0ba2823",
|
|
|
"metadata": {},
|
|
|
"outputs": [
|
|
|
{
|
|
|
"name": "stdout",
|
|
|
"output_type": "stream",
|
|
|
"text": [
|
|
|
+ " * sqlite:///chinook.db\n",
|
|
|
"Done.\n"
|
|
|
]
|
|
|
},
|
|
@@ -153,61 +154,65 @@
|
|
|
"data": {
|
|
|
"text/html": [
|
|
|
"<table>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <th>genre</th>\n",
|
|
|
- " <th>tracks_sold</th>\n",
|
|
|
- " <th>percentage_sold</th>\n",
|
|
|
- " </tr>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <td>Rock</td>\n",
|
|
|
- " <td>561</td>\n",
|
|
|
- " <td>0.5337773549000951</td>\n",
|
|
|
- " </tr>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <td>Alternative & Punk</td>\n",
|
|
|
- " <td>130</td>\n",
|
|
|
- " <td>0.12369172216936251</td>\n",
|
|
|
- " </tr>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <td>Metal</td>\n",
|
|
|
- " <td>124</td>\n",
|
|
|
- " <td>0.11798287345385347</td>\n",
|
|
|
- " </tr>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <td>R&B/Soul</td>\n",
|
|
|
- " <td>53</td>\n",
|
|
|
- " <td>0.05042816365366318</td>\n",
|
|
|
- " </tr>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <td>Blues</td>\n",
|
|
|
- " <td>36</td>\n",
|
|
|
- " <td>0.03425309229305423</td>\n",
|
|
|
- " </tr>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <td>Alternative</td>\n",
|
|
|
- " <td>35</td>\n",
|
|
|
- " <td>0.03330161750713606</td>\n",
|
|
|
- " </tr>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <td>Latin</td>\n",
|
|
|
- " <td>22</td>\n",
|
|
|
- " <td>0.02093244529019981</td>\n",
|
|
|
- " </tr>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <td>Pop</td>\n",
|
|
|
- " <td>22</td>\n",
|
|
|
- " <td>0.02093244529019981</td>\n",
|
|
|
- " </tr>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <td>Hip Hop/Rap</td>\n",
|
|
|
- " <td>20</td>\n",
|
|
|
- " <td>0.019029495718363463</td>\n",
|
|
|
- " </tr>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <td>Jazz</td>\n",
|
|
|
- " <td>14</td>\n",
|
|
|
- " <td>0.013320647002854425</td>\n",
|
|
|
- " </tr>\n",
|
|
|
+ " <thead>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <th>genre</th>\n",
|
|
|
+ " <th>tracks_sold</th>\n",
|
|
|
+ " <th>percentage_sold</th>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " </thead>\n",
|
|
|
+ " <tbody>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <td>Rock</td>\n",
|
|
|
+ " <td>561</td>\n",
|
|
|
+ " <td>0.5337773549000951</td>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <td>Alternative & Punk</td>\n",
|
|
|
+ " <td>130</td>\n",
|
|
|
+ " <td>0.12369172216936251</td>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <td>Metal</td>\n",
|
|
|
+ " <td>124</td>\n",
|
|
|
+ " <td>0.11798287345385347</td>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <td>R&B/Soul</td>\n",
|
|
|
+ " <td>53</td>\n",
|
|
|
+ " <td>0.05042816365366318</td>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <td>Blues</td>\n",
|
|
|
+ " <td>36</td>\n",
|
|
|
+ " <td>0.03425309229305423</td>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <td>Alternative</td>\n",
|
|
|
+ " <td>35</td>\n",
|
|
|
+ " <td>0.03330161750713606</td>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <td>Pop</td>\n",
|
|
|
+ " <td>22</td>\n",
|
|
|
+ " <td>0.02093244529019981</td>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <td>Latin</td>\n",
|
|
|
+ " <td>22</td>\n",
|
|
|
+ " <td>0.02093244529019981</td>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <td>Hip Hop/Rap</td>\n",
|
|
|
+ " <td>20</td>\n",
|
|
|
+ " <td>0.019029495718363463</td>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <td>Jazz</td>\n",
|
|
|
+ " <td>14</td>\n",
|
|
|
+ " <td>0.013320647002854425</td>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " </tbody>\n",
|
|
|
"</table>"
|
|
|
],
|
|
|
"text/plain": [
|
|
@@ -217,8 +222,8 @@
|
|
|
" ('R&B/Soul', 53, 0.05042816365366318),\n",
|
|
|
" ('Blues', 36, 0.03425309229305423),\n",
|
|
|
" ('Alternative', 35, 0.03330161750713606),\n",
|
|
|
- " ('Latin', 22, 0.02093244529019981),\n",
|
|
|
" ('Pop', 22, 0.02093244529019981),\n",
|
|
|
+ " ('Latin', 22, 0.02093244529019981),\n",
|
|
|
" ('Hip Hop/Rap', 20, 0.019029495718363463),\n",
|
|
|
" ('Jazz', 14, 0.013320647002854425)]"
|
|
|
]
|
|
@@ -255,6 +260,7 @@
|
|
|
},
|
|
|
{
|
|
|
"cell_type": "markdown",
|
|
|
+ "id": "4d29e05f",
|
|
|
"metadata": {},
|
|
|
"source": [
|
|
|
"Based on the sales of tracks across different genres in the USA, we should purchase the new albums by the following artists:\n",
|
|
@@ -268,6 +274,7 @@
|
|
|
},
|
|
|
{
|
|
|
"cell_type": "markdown",
|
|
|
+ "id": "76438863",
|
|
|
"metadata": {},
|
|
|
"source": [
|
|
|
"## Analyzing Employee Sales Performance"
|
|
@@ -276,12 +283,14 @@
|
|
|
{
|
|
|
"cell_type": "code",
|
|
|
"execution_count": 4,
|
|
|
+ "id": "d24b70c2",
|
|
|
"metadata": {},
|
|
|
"outputs": [
|
|
|
{
|
|
|
"name": "stdout",
|
|
|
"output_type": "stream",
|
|
|
"text": [
|
|
|
+ " * sqlite:///chinook.db\n",
|
|
|
"Done.\n"
|
|
|
]
|
|
|
},
|
|
@@ -289,26 +298,30 @@
|
|
|
"data": {
|
|
|
"text/html": [
|
|
|
"<table>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <th>employee</th>\n",
|
|
|
- " <th>hire_date</th>\n",
|
|
|
- " <th>total_sales</th>\n",
|
|
|
- " </tr>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <td>Jane Peacock</td>\n",
|
|
|
- " <td>2017-04-01 00:00:00</td>\n",
|
|
|
- " <td>1731.5099999999998</td>\n",
|
|
|
- " </tr>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <td>Margaret Park</td>\n",
|
|
|
- " <td>2017-05-03 00:00:00</td>\n",
|
|
|
- " <td>1584.0000000000002</td>\n",
|
|
|
- " </tr>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <td>Steve Johnson</td>\n",
|
|
|
- " <td>2017-10-17 00:00:00</td>\n",
|
|
|
- " <td>1393.92</td>\n",
|
|
|
- " </tr>\n",
|
|
|
+ " <thead>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <th>employee</th>\n",
|
|
|
+ " <th>hire_date</th>\n",
|
|
|
+ " <th>total_sales</th>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " </thead>\n",
|
|
|
+ " <tbody>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <td>Jane Peacock</td>\n",
|
|
|
+ " <td>2017-04-01 00:00:00</td>\n",
|
|
|
+ " <td>1731.5099999999998</td>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <td>Margaret Park</td>\n",
|
|
|
+ " <td>2017-05-03 00:00:00</td>\n",
|
|
|
+ " <td>1584.0000000000002</td>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <td>Steve Johnson</td>\n",
|
|
|
+ " <td>2017-10-17 00:00:00</td>\n",
|
|
|
+ " <td>1393.92</td>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " </tbody>\n",
|
|
|
"</table>"
|
|
|
],
|
|
|
"text/plain": [
|
|
@@ -347,6 +360,7 @@
|
|
|
},
|
|
|
{
|
|
|
"cell_type": "markdown",
|
|
|
+ "id": "0b2d61eb",
|
|
|
"metadata": {},
|
|
|
"source": [
|
|
|
"While there is a 20% difference in sales between Jane (the top employee) and Steve (the bottom employee), the difference roughly corresponds to the differences in their hiring dates."
|
|
@@ -354,6 +368,7 @@
|
|
|
},
|
|
|
{
|
|
|
"cell_type": "markdown",
|
|
|
+ "id": "04acc888",
|
|
|
"metadata": {},
|
|
|
"source": [
|
|
|
"## Analyzing Sales by Country"
|
|
@@ -362,12 +377,14 @@
|
|
|
{
|
|
|
"cell_type": "code",
|
|
|
"execution_count": 5,
|
|
|
+ "id": "3728afb0",
|
|
|
"metadata": {},
|
|
|
"outputs": [
|
|
|
{
|
|
|
"name": "stdout",
|
|
|
"output_type": "stream",
|
|
|
"text": [
|
|
|
+ " * sqlite:///chinook.db\n",
|
|
|
"Done.\n"
|
|
|
]
|
|
|
},
|
|
@@ -375,83 +392,87 @@
|
|
|
"data": {
|
|
|
"text/html": [
|
|
|
"<table>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <th>country</th>\n",
|
|
|
- " <th>customers</th>\n",
|
|
|
- " <th>total_sales</th>\n",
|
|
|
- " <th>average_order</th>\n",
|
|
|
- " <th>customer_lifetime_value</th>\n",
|
|
|
- " </tr>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <td>USA</td>\n",
|
|
|
- " <td>13</td>\n",
|
|
|
- " <td>1040.490000000008</td>\n",
|
|
|
- " <td>7.942671755725252</td>\n",
|
|
|
- " <td>80.03769230769292</td>\n",
|
|
|
- " </tr>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <td>Canada</td>\n",
|
|
|
- " <td>8</td>\n",
|
|
|
- " <td>535.5900000000034</td>\n",
|
|
|
- " <td>7.047236842105309</td>\n",
|
|
|
- " <td>66.94875000000043</td>\n",
|
|
|
- " </tr>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <td>Brazil</td>\n",
|
|
|
- " <td>5</td>\n",
|
|
|
- " <td>427.68000000000245</td>\n",
|
|
|
- " <td>7.011147540983647</td>\n",
|
|
|
- " <td>85.53600000000048</td>\n",
|
|
|
- " </tr>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <td>France</td>\n",
|
|
|
- " <td>5</td>\n",
|
|
|
- " <td>389.0700000000021</td>\n",
|
|
|
- " <td>7.781400000000042</td>\n",
|
|
|
- " <td>77.81400000000042</td>\n",
|
|
|
- " </tr>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <td>Germany</td>\n",
|
|
|
- " <td>4</td>\n",
|
|
|
- " <td>334.6200000000016</td>\n",
|
|
|
- " <td>8.161463414634186</td>\n",
|
|
|
- " <td>83.6550000000004</td>\n",
|
|
|
- " </tr>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <td>Czech Republic</td>\n",
|
|
|
- " <td>2</td>\n",
|
|
|
- " <td>273.24000000000103</td>\n",
|
|
|
- " <td>9.108000000000034</td>\n",
|
|
|
- " <td>136.62000000000052</td>\n",
|
|
|
- " </tr>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <td>United Kingdom</td>\n",
|
|
|
- " <td>3</td>\n",
|
|
|
- " <td>245.52000000000078</td>\n",
|
|
|
- " <td>8.768571428571457</td>\n",
|
|
|
- " <td>81.84000000000026</td>\n",
|
|
|
- " </tr>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <td>Portugal</td>\n",
|
|
|
- " <td>2</td>\n",
|
|
|
- " <td>185.13000000000022</td>\n",
|
|
|
- " <td>6.3837931034482835</td>\n",
|
|
|
- " <td>92.56500000000011</td>\n",
|
|
|
- " </tr>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <td>India</td>\n",
|
|
|
- " <td>2</td>\n",
|
|
|
- " <td>183.1500000000002</td>\n",
|
|
|
- " <td>8.72142857142858</td>\n",
|
|
|
- " <td>91.5750000000001</td>\n",
|
|
|
- " </tr>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <td>Other</td>\n",
|
|
|
- " <td>15</td>\n",
|
|
|
- " <td>1094.9400000000085</td>\n",
|
|
|
- " <td>7.448571428571486</td>\n",
|
|
|
- " <td>72.99600000000056</td>\n",
|
|
|
- " </tr>\n",
|
|
|
+ " <thead>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <th>country</th>\n",
|
|
|
+ " <th>customers</th>\n",
|
|
|
+ " <th>total_sales</th>\n",
|
|
|
+ " <th>average_order</th>\n",
|
|
|
+ " <th>customer_lifetime_value</th>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " </thead>\n",
|
|
|
+ " <tbody>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <td>USA</td>\n",
|
|
|
+ " <td>13</td>\n",
|
|
|
+ " <td>1040.490000000008</td>\n",
|
|
|
+ " <td>7.942671755725252</td>\n",
|
|
|
+ " <td>80.03769230769292</td>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <td>Canada</td>\n",
|
|
|
+ " <td>8</td>\n",
|
|
|
+ " <td>535.5900000000034</td>\n",
|
|
|
+ " <td>7.047236842105309</td>\n",
|
|
|
+ " <td>66.94875000000043</td>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <td>Brazil</td>\n",
|
|
|
+ " <td>5</td>\n",
|
|
|
+ " <td>427.68000000000245</td>\n",
|
|
|
+ " <td>7.011147540983647</td>\n",
|
|
|
+ " <td>85.53600000000048</td>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <td>France</td>\n",
|
|
|
+ " <td>5</td>\n",
|
|
|
+ " <td>389.0700000000021</td>\n",
|
|
|
+ " <td>7.781400000000042</td>\n",
|
|
|
+ " <td>77.81400000000042</td>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <td>Germany</td>\n",
|
|
|
+ " <td>4</td>\n",
|
|
|
+ " <td>334.6200000000016</td>\n",
|
|
|
+ " <td>8.161463414634186</td>\n",
|
|
|
+ " <td>83.6550000000004</td>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <td>Czech Republic</td>\n",
|
|
|
+ " <td>2</td>\n",
|
|
|
+ " <td>273.24000000000103</td>\n",
|
|
|
+ " <td>9.108000000000034</td>\n",
|
|
|
+ " <td>136.62000000000052</td>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <td>United Kingdom</td>\n",
|
|
|
+ " <td>3</td>\n",
|
|
|
+ " <td>245.52000000000078</td>\n",
|
|
|
+ " <td>8.768571428571457</td>\n",
|
|
|
+ " <td>81.84000000000026</td>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <td>Portugal</td>\n",
|
|
|
+ " <td>2</td>\n",
|
|
|
+ " <td>185.13000000000022</td>\n",
|
|
|
+ " <td>6.3837931034482835</td>\n",
|
|
|
+ " <td>92.56500000000011</td>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <td>India</td>\n",
|
|
|
+ " <td>2</td>\n",
|
|
|
+ " <td>183.1500000000002</td>\n",
|
|
|
+ " <td>8.72142857142858</td>\n",
|
|
|
+ " <td>91.5750000000001</td>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <td>Other</td>\n",
|
|
|
+ " <td>15</td>\n",
|
|
|
+ " <td>1094.9400000000085</td>\n",
|
|
|
+ " <td>7.448571428571486</td>\n",
|
|
|
+ " <td>72.99600000000056</td>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " </tbody>\n",
|
|
|
"</table>"
|
|
|
],
|
|
|
"text/plain": [
|
|
@@ -519,6 +540,7 @@
|
|
|
},
|
|
|
{
|
|
|
"cell_type": "markdown",
|
|
|
+ "id": "fc607eb9",
|
|
|
"metadata": {},
|
|
|
"source": [
|
|
|
"Based on the data, there may be opportunity in the following countries:\n",
|
|
@@ -532,6 +554,7 @@
|
|
|
},
|
|
|
{
|
|
|
"cell_type": "markdown",
|
|
|
+ "id": "d58ca751",
|
|
|
"metadata": {},
|
|
|
"source": [
|
|
|
"## Albums vs. Individual Tracks"
|
|
@@ -540,12 +563,14 @@
|
|
|
{
|
|
|
"cell_type": "code",
|
|
|
"execution_count": 6,
|
|
|
+ "id": "013b4aea",
|
|
|
"metadata": {},
|
|
|
"outputs": [
|
|
|
{
|
|
|
"name": "stdout",
|
|
|
"output_type": "stream",
|
|
|
"text": [
|
|
|
+ " * sqlite:///chinook.db\n",
|
|
|
"Done.\n"
|
|
|
]
|
|
|
},
|
|
@@ -553,21 +578,25 @@
|
|
|
"data": {
|
|
|
"text/html": [
|
|
|
"<table>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <th>album_purchase</th>\n",
|
|
|
- " <th>number_of_invoices</th>\n",
|
|
|
- " <th>percent</th>\n",
|
|
|
- " </tr>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <td>no</td>\n",
|
|
|
- " <td>500</td>\n",
|
|
|
- " <td>0.8143322475570033</td>\n",
|
|
|
- " </tr>\n",
|
|
|
- " <tr>\n",
|
|
|
- " <td>yes</td>\n",
|
|
|
- " <td>114</td>\n",
|
|
|
- " <td>0.18566775244299674</td>\n",
|
|
|
- " </tr>\n",
|
|
|
+ " <thead>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <th>album_purchase</th>\n",
|
|
|
+ " <th>number_of_invoices</th>\n",
|
|
|
+ " <th>percent</th>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " </thead>\n",
|
|
|
+ " <tbody>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <td>no</td>\n",
|
|
|
+ " <td>500</td>\n",
|
|
|
+ " <td>0.8143322475570033</td>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " <tr>\n",
|
|
|
+ " <td>yes</td>\n",
|
|
|
+ " <td>114</td>\n",
|
|
|
+ " <td>0.18566775244299674</td>\n",
|
|
|
+ " </tr>\n",
|
|
|
+ " </tbody>\n",
|
|
|
"</table>"
|
|
|
],
|
|
|
"text/plain": [
|
|
@@ -580,82 +609,104 @@
|
|
|
}
|
|
|
],
|
|
|
"source": [
|
|
|
+ "\n",
|
|
|
"%%sql\n",
|
|
|
"\n",
|
|
|
- "WITH invoice_first_track AS\n",
|
|
|
- " (\n",
|
|
|
- " SELECT\n",
|
|
|
- " il.invoice_id invoice_id,\n",
|
|
|
- " MIN(il.track_id) first_track_id\n",
|
|
|
- " FROM invoice_line il\n",
|
|
|
- " GROUP BY 1\n",
|
|
|
- " )\n",
|
|
|
+ "WITH invoice_first_track AS (\n",
|
|
|
+ " SELECT\n",
|
|
|
+ " il.invoice_id AS invoice_id,\n",
|
|
|
+ " MIN(il.track_id) AS first_track_id\n",
|
|
|
+ " FROM\n",
|
|
|
+ " invoice_line il\n",
|
|
|
+ " GROUP BY\n",
|
|
|
+ " 1\n",
|
|
|
+ ")\n",
|
|
|
"\n",
|
|
|
+ "-- Use a subquery to select the results of the invoice_first_track CTE and determine whether customers made album purchases\n",
|
|
|
"SELECT\n",
|
|
|
- " album_purchase,\n",
|
|
|
- " COUNT(invoice_id) number_of_invoices,\n",
|
|
|
- " CAST(count(invoice_id) AS FLOAT) / (\n",
|
|
|
- " SELECT COUNT(*) FROM invoice\n",
|
|
|
- " ) percent\n",
|
|
|
+ " album_purchase,\n",
|
|
|
+ " COUNT(invoice_id) AS number_of_invoices,\n",
|
|
|
+ " CAST(COUNT(invoice_id) AS FLOAT) / (\n",
|
|
|
+ " SELECT COUNT(*) FROM invoice\n",
|
|
|
+ " ) AS percent\n",
|
|
|
"FROM\n",
|
|
|
- " (\n",
|
|
|
+ " (\n",
|
|
|
" SELECT\n",
|
|
|
- " ifs.*,\n",
|
|
|
- " CASE\n",
|
|
|
- " WHEN\n",
|
|
|
- " (\n",
|
|
|
- " SELECT t.track_id FROM track t\n",
|
|
|
- " WHERE t.album_id = (\n",
|
|
|
- " SELECT t2.album_id FROM track t2\n",
|
|
|
- " WHERE t2.track_id = ifs.first_track_id\n",
|
|
|
- " ) \n",
|
|
|
- "\n",
|
|
|
- " EXCEPT \n",
|
|
|
- "\n",
|
|
|
- " SELECT il2.track_id FROM invoice_line il2\n",
|
|
|
- " WHERE il2.invoice_id = ifs.invoice_id\n",
|
|
|
- " ) IS NULL\n",
|
|
|
- " AND\n",
|
|
|
- " (\n",
|
|
|
- " SELECT il2.track_id FROM invoice_line il2\n",
|
|
|
- " WHERE il2.invoice_id = ifs.invoice_id\n",
|
|
|
- "\n",
|
|
|
- " EXCEPT \n",
|
|
|
- "\n",
|
|
|
- " SELECT t.track_id FROM track t\n",
|
|
|
- " WHERE t.album_id = (\n",
|
|
|
- " SELECT t2.album_id FROM track t2\n",
|
|
|
- " WHERE t2.track_id = ifs.first_track_id\n",
|
|
|
- " ) \n",
|
|
|
- " ) IS NULL\n",
|
|
|
- " THEN \"yes\"\n",
|
|
|
- " ELSE \"no\"\n",
|
|
|
- " END AS \"album_purchase\"\n",
|
|
|
- " FROM invoice_first_track ifs\n",
|
|
|
- " )\n",
|
|
|
- "GROUP BY album_purchase;"
|
|
|
+ " ifs.*,\n",
|
|
|
+ " CASE\n",
|
|
|
+ " -- Use the EXCEPT operator to compare the tracks in the first invoice with the tracks in subsequent invoices,\n",
|
|
|
+ " -- and determine whether any tracks from the album were purchased in subsequent invoices.\n",
|
|
|
+ " -- If the result of the EXCEPT is NULL, it means that all tracks from the album were purchased in subsequent invoices,\n",
|
|
|
+ " -- and the customer made an album purchase.\n",
|
|
|
+ " -- If the result of the EXCEPT is not NULL, it means that at least one track from the album was not purchased in subsequent invoices,\n",
|
|
|
+ " -- and the customer did not make an album purchase.\n",
|
|
|
+ " WHEN (\n",
|
|
|
+ " SELECT\n",
|
|
|
+ " t.track_id\n",
|
|
|
+ " FROM\n",
|
|
|
+ " track t\n",
|
|
|
+ " WHERE\n",
|
|
|
+ " t.album_id = (\n",
|
|
|
+ " SELECT\n",
|
|
|
+ " t2.album_id\n",
|
|
|
+ " FROM\n",
|
|
|
+ " track t2\n",
|
|
|
+ " WHERE\n",
|
|
|
+ " t2.track_id = ifs.first_track_id\n",
|
|
|
+ " )\n",
|
|
|
+ " EXCEPT\n",
|
|
|
+ " SELECT\n",
|
|
|
+ " il2.track_id\n",
|
|
|
+ " FROM\n",
|
|
|
+ " invoice_line il2\n",
|
|
|
+ " WHERE\n",
|
|
|
+ " il2.invoice_id = ifs.invoice_id\n",
|
|
|
+ " ) IS NULL\n",
|
|
|
+ " AND (\n",
|
|
|
+ " SELECT\n",
|
|
|
+ " il2.track_id\n",
|
|
|
+ " FROM\n",
|
|
|
+ " invoice_line il2\n",
|
|
|
+ " WHERE\n",
|
|
|
+ " il2.invoice_id = ifs.invoice_id\n",
|
|
|
+ " EXCEPT\n",
|
|
|
+ " SELECT\n",
|
|
|
+ " t.track_id\n",
|
|
|
+ " FROM\n",
|
|
|
+ " track t\n",
|
|
|
+ " WHERE\n",
|
|
|
+ " t.album_id = (\n",
|
|
|
+ " SELECT\n",
|
|
|
+ " t2.album_id\n",
|
|
|
+ " FROM\n",
|
|
|
+ " track t2\n",
|
|
|
+ " WHERE\n",
|
|
|
+ " t2.track_id = ifs.first_track_id\n",
|
|
|
+ " )\n",
|
|
|
+ " ) IS NULL\n",
|
|
|
+ " THEN \"yes\"\n",
|
|
|
+ " ELSE \"no\"\n",
|
|
|
+ " END AS album_purchase\n",
|
|
|
+ " FROM\n",
|
|
|
+ " invoice_first_track ifs\n",
|
|
|
+ " ) subquery\n",
|
|
|
+ "-- Group by album_purchase to get the counts and percentages for each type of purchase\n",
|
|
|
+ "GROUP BY\n",
|
|
|
+ " album_purchase;\n"
|
|
|
]
|
|
|
},
|
|
|
{
|
|
|
"cell_type": "markdown",
|
|
|
+ "id": "1da400b1",
|
|
|
"metadata": {},
|
|
|
"source": [
|
|
|
- "Album purchases account for 18.6% of purchases. Based on this data, I would recommend against purchasing only select tracks from albums from record companies, since there is potential to lose one fifth of revenue."
|
|
|
+ "Album purchases account for 18.6% of purchases. Based on this data, I would recommend against purchasing only select tracks from albums from record companies, since there is potential to lose one fifth of revenue"
|
|
|
]
|
|
|
- },
|
|
|
- {
|
|
|
- "cell_type": "code",
|
|
|
- "execution_count": null,
|
|
|
- "metadata": {
|
|
|
- "collapsed": true
|
|
|
- },
|
|
|
- "outputs": [],
|
|
|
- "source": []
|
|
|
}
|
|
|
],
|
|
|
"metadata": {
|
|
|
"kernelspec": {
|
|
|
- "display_name": "Python 3",
|
|
|
+ "display_name": "Python 3 (ipykernel)",
|
|
|
"language": "python",
|
|
|
"name": "python3"
|
|
|
},
|
|
@@ -669,9 +720,9 @@
|
|
|
"name": "python",
|
|
|
"nbconvert_exporter": "python",
|
|
|
"pygments_lexer": "ipython3",
|
|
|
- "version": "3.8.5"
|
|
|
+ "version": "3.9.7"
|
|
|
}
|
|
|
},
|
|
|
"nbformat": 4,
|
|
|
- "nbformat_minor": 2
|
|
|
+ "nbformat_minor": 5
|
|
|
}
|