\n",
" "
],
"text/plain": [
" second_most_expensive\n",
"0 96.51"
]
},
"metadata": {},
"execution_count": 5
}
]
},
{
"cell_type": "markdown",
"source": [
"BTW, this is not nessecarily the way I would have done this. (Rank = 2 might have worked well (still would have required a sub-query though!)\n",
"\n"
],
"metadata": {
"id": "BDGdqDpiKUVX"
}
},
{
"cell_type": "code",
"source": [
"%%bigquery --project pic-math\n",
"WITH table as (\n",
"SELECT state_bottle_retail, RANK() OVER(ORDER BY state_bottle_retail DESC) as rank_by_price\n",
"FROM `bigquery-public-data.iowa_liquor_sales.sales`\n",
"WHERE category_name = \"DISTILLED SPIRITS SPECIALTY\"\n",
"GROUP BY state_bottle_retail\n",
")\n",
"\n",
"SELECT state_bottle_retail as second_highest_price\n",
"FROM table\n",
"WHERE rank_by_price = 2\n",
"ORDER BY rank_by_price DESC\n",
"\n"
],
"metadata": {
"id": "pGcytGfVgpuq",
"outputId": "9fe825d1-0f2f-4363-d0d2-98b58bebd81e",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 81
}
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/html": [
"\n",
"
\n",
"
\n",
"
\n",
"\n",
"
\n",
" \n",
"
\n",
"
\n",
"
second_highest_price
\n",
"
\n",
" \n",
" \n",
"
\n",
"
0
\n",
"
96.51
\n",
"
\n",
" \n",
"
\n",
"
\n",
" \n",
" \n",
" \n",
"\n",
" \n",
"
\n",
"
\n",
" "
],
"text/plain": [
" second_highest_price\n",
"0 96.51"
]
},
"metadata": {},
"execution_count": 24
}
]
},
{
"cell_type": "markdown",
"source": [
"Let's try another. What if we want to find how many bottles in a category are over $100."
],
"metadata": {
"id": "JTNJhzIpkY5e"
}
},
{
"cell_type": "code",
"source": [
"%%bigquery --project pic-math\n",
"\n",
"SELECT category_name, COUNTIF(state_bottle_retail>100) as number_bottles_over_benjamin\n",
"FROM `bigquery-public-data.iowa_liquor_sales.sales`\n",
"GROUP BY category_name, item_description\n",
"Order BY number_bottles_over_benjamin DESC"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 424
},
"id": "yfcCT2f4yAkE",
"outputId": "54daa066-a744-497e-e16a-6b4e91ddb6fa"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/html": [
"\n",
"
\n",
"
\n",
"
\n",
"\n",
"
\n",
" \n",
"
\n",
"
\n",
"
category_name
\n",
"
number_bottles_over_benjamin
\n",
"
\n",
" \n",
" \n",
"
\n",
"
0
\n",
"
Scotch Whiskies
\n",
"
2552
\n",
"
\n",
"
\n",
"
1
\n",
"
SCOTCH WHISKIES
\n",
"
1897
\n",
"
\n",
"
\n",
"
2
\n",
"
100% Agave Tequila
\n",
"
1579
\n",
"
\n",
"
\n",
"
3
\n",
"
SINGLE MALT SCOTCH
\n",
"
1119
\n",
"
\n",
"
\n",
"
4
\n",
"
Imported Brandies
\n",
"
1090
\n",
"
\n",
"
\n",
"
...
\n",
"
...
\n",
"
...
\n",
"
\n",
"
\n",
"
13825
\n",
"
Straight Bourbon Whiskies
\n",
"
0
\n",
"
\n",
"
\n",
"
13826
\n",
"
American Cordials & Liqueur
\n",
"
0
\n",
"
\n",
"
\n",
"
13827
\n",
"
American Cordials & Liqueur
\n",
"
0
\n",
"
\n",
"
\n",
"
13828
\n",
"
MISC. AMERICAN CORDIALS & LIQUEURS
\n",
"
0
\n",
"
\n",
"
\n",
"
13829
\n",
"
MISC. IMPORTED CORDIALS & LIQUEURS
\n",
"
0
\n",
"
\n",
" \n",
"
\n",
"
13830 rows × 2 columns
\n",
"
\n",
" \n",
" \n",
" \n",
"\n",
" \n",
"
\n",
"
\n",
" "
],
"text/plain": [
" category_name number_bottles_over_benjamin\n",
"0 Scotch Whiskies 2552\n",
"1 SCOTCH WHISKIES 1897\n",
"2 100% Agave Tequila 1579\n",
"3 SINGLE MALT SCOTCH 1119\n",
"4 Imported Brandies 1090\n",
"... ... ...\n",
"13825 Straight Bourbon Whiskies 0\n",
"13826 American Cordials & Liqueur 0\n",
"13827 American Cordials & Liqueur 0\n",
"13828 MISC. AMERICAN CORDIALS & LIQUEURS 0\n",
"13829 MISC. IMPORTED CORDIALS & LIQUEURS 0\n",
"\n",
"[13830 rows x 2 columns]"
]
},
"metadata": {},
"execution_count": 25
}
]
},
{
"cell_type": "markdown",
"source": [
"I think the book I am working from pre-dates `COUNTIF`. Let me show you a sub-query solution.\n",
"\n",
"I'll make a column asking if the bottle is over $100."
],
"metadata": {
"id": "KoHB_p3rLSd9"
}
},
{
"cell_type": "code",
"source": [
"%%bigquery --project pic-math\n",
"\n",
"SELECT *, (CASE WHEN state_bottle_retail >100 THEN 1 ELSE 0 END) as over_benjamin\n",
"FROM `bigquery-public-data.iowa_liquor_sales.sales`\n",
"LIMIT 10"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 809
},
"id": "kAkphP8PK1uQ",
"outputId": "9aa0a210-0411-4a52-a1f8-0317f249b617"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/html": [
"\n",
"
\n",
"
\n",
"
\n",
"\n",
"
\n",
" \n",
"
\n",
"
\n",
"
invoice_and_item_number
\n",
"
date
\n",
"
store_number
\n",
"
store_name
\n",
"
address
\n",
"
city
\n",
"
zip_code
\n",
"
store_location
\n",
"
county_number
\n",
"
county
\n",
"
category
\n",
"
category_name
\n",
"
vendor_number
\n",
"
vendor_name
\n",
"
item_number
\n",
"
item_description
\n",
"
pack
\n",
"
bottle_volume_ml
\n",
"
state_bottle_cost
\n",
"
state_bottle_retail
\n",
"
bottles_sold
\n",
"
sale_dollars
\n",
"
volume_sold_liters
\n",
"
volume_sold_gallons
\n",
"
over_benjamin
\n",
"
\n",
" \n",
" \n",
"
\n",
"
0
\n",
"
INV-37606800019
\n",
"
2021-06-18
\n",
"
2190
\n",
"
Central City Liquor, Inc.
\n",
"
1460 2ND AVE
\n",
"
Des Moines
\n",
"
50314.0
\n",
"
POINT (-93.619787 41.60566)
\n",
"
77
\n",
"
POLK
\n",
"
1082100.0
\n",
"
Imported Cordials & Liqueur
\n",
"
619
\n",
"
Campari America
\n",
"
65127
\n",
"
Grand Marnier Cordon Rouge
\n",
"
6
\n",
"
1000
\n",
"
25.55
\n",
"
38.33
\n",
"
2
\n",
"
76.66
\n",
"
2.00
\n",
"
0.52
\n",
"
0
\n",
"
\n",
"
\n",
"
1
\n",
"
INV-19821600132
\n",
"
2019-06-05
\n",
"
2629
\n",
"
Hy-Vee Food Store #2 / Council Bluffs
\n",
"
1745 Madison Ave
\n",
"
Council Bluffs
\n",
"
51503
\n",
"
POINT (-95.825137 41.242732)
\n",
"
78
\n",
"
POTTAWATTA
\n",
"
None
\n",
"
None
\n",
"
55
\n",
"
SAZERAC NORTH AMERICA
\n",
"
84197
\n",
"
99 Pineapple Mini
\n",
"
10
\n",
"
600
\n",
"
6.00
\n",
"
9.00
\n",
"
2
\n",
"
18.00
\n",
"
1.20
\n",
"
0.31
\n",
"
0
\n",
"
\n",
"
\n",
"
2
\n",
"
INV-20266400001
\n",
"
2019-06-27
\n",
"
3713
\n",
"
Wal-Mart 0810 / Mason City
\n",
"
4151 4th St SW
\n",
"
Mason City
\n",
"
50401
\n",
"
POINT (-93.261648 43.148239)
\n",
"
17
\n",
"
CERRO GORD
\n",
"
1062300.0
\n",
"
Aged Dark Rum
\n",
"
482
\n",
"
Sovereign Brands, LLC
\n",
"
64529
\n",
"
Bumbu Rum
\n",
"
6
\n",
"
750
\n",
"
18.50
\n",
"
27.75
\n",
"
18
\n",
"
499.50
\n",
"
13.50
\n",
"
3.56
\n",
"
0
\n",
"
\n",
"
\n",
"
3
\n",
"
INV-13387900079
\n",
"
2018-07-23
\n",
"
4829
\n",
"
Central City 2
\n",
"
1501 Michigan Ave
\n",
"
Des Moines
\n",
"
50314
\n",
"
POINT (-93.613739 41.60572)
\n",
"
77
\n",
"
POLK
\n",
"
1081500.0
\n",
"
Triple Sec
\n",
"
434
\n",
"
LUXCO INC
\n",
"
86251
\n",
"
Juarez Triple Sec
\n",
"
12
\n",
"
1000
\n",
"
2.42
\n",
"
3.63
\n",
"
180
\n",
"
653.40
\n",
"
180.00
\n",
"
47.55
\n",
"
0
\n",
"
\n",
"
\n",
"
4
\n",
"
INV-13998900081
\n",
"
2018-08-22
\n",
"
5257
\n",
"
MAD Ave Quik Shop
\n",
"
405, Madison Ave
\n",
"
Ottumwa
\n",
"
52501
\n",
"
None
\n",
"
90
\n",
"
WAPELLO
\n",
"
1092100.0
\n",
"
Imported Distilled Spirit Specialty
\n",
"
55.0
\n",
"
SAZERAC NORTH AMERICA
\n",
"
76227
\n",
"
Montezuma Blue
\n",
"
12
\n",
"
1000
\n",
"
4.66
\n",
"
6.99
\n",
"
120
\n",
"
838.80
\n",
"
120.00
\n",
"
31.70
\n",
"
0
\n",
"
\n",
"
\n",
"
5
\n",
"
S21846500003
\n",
"
2014-10-20
\n",
"
3618
\n",
"
Wal-Mart 2716 / Cedar Rapids
\n",
"
3601 29TH AVE SW
\n",
"
CEDAR RAPIDS
\n",
"
52404
\n",
"
POINT (-91.718042 41.949215)
\n",
"
57
\n",
"
Linn
\n",
"
1701100.0
\n",
"
DECANTERS & SPECIALTY PACKAGES
\n",
"
65
\n",
"
Jim Beam Brands
\n",
"
789
\n",
"
Canadian Club w/Glass
\n",
"
6
\n",
"
750
\n",
"
7.88
\n",
"
11.82
\n",
"
30
\n",
"
354.60
\n",
"
22.50
\n",
"
5.94
\n",
"
0
\n",
"
\n",
"
\n",
"
6
\n",
"
S26037800001
\n",
"
2015-06-09
\n",
"
3944
\n",
"
Sam's Club 4973 / Dubuque
\n",
"
4400 ASBURY RD
\n",
"
DUBUQUE
\n",
"
52002
\n",
"
POINT (-90.737821 42.515289)
\n",
"
31
\n",
"
Dubuque
\n",
"
1701100.0
\n",
"
DECANTERS & SPECIALTY PACKAGES
\n",
"
65
\n",
"
Jim Beam Brands
\n",
"
19082
\n",
"
Jim Beam Operation Homefront
\n",
"
6
\n",
"
1750
\n",
"
18.37
\n",
"
27.56
\n",
"
210
\n",
"
5787.60
\n",
"
367.50
\n",
"
97.08
\n",
"
0
\n",
"
\n",
"
\n",
"
7
\n",
"
INV-06908200123
\n",
"
2017-08-28
\n",
"
3612
\n",
"
B and C Liquor / Maquoketa
\n",
"
509 E Platt
\n",
"
Maquoketa
\n",
"
52060.0
\n",
"
POINT (-90.659692 42.06922000000001)
\n",
"
49
\n",
"
JACKSON
\n",
"
1062100.0
\n",
"
Gold Rum
\n",
"
434
\n",
"
LUXCO INC
\n",
"
45248
\n",
"
Paramount Gold Rum
\n",
"
6
\n",
"
1750
\n",
"
7.84
\n",
"
11.76
\n",
"
3
\n",
"
35.28
\n",
"
5.25
\n",
"
1.39
\n",
"
0
\n",
"
\n",
"
\n",
"
8
\n",
"
INV-20029700068
\n",
"
2019-06-17
\n",
"
2576
\n",
"
Hy-Vee Wine and Spirits / Storm Lake
\n",
"
1250 N Lake St
\n",
"
Storm Lake
\n",
"
50588
\n",
"
POINT (-95.200758 42.65318400000001)
\n",
"
11
\n",
"
BUENA VIST
\n",
"
1062300.0
\n",
"
Aged Dark Rum
\n",
"
65
\n",
"
Jim Beam Brands
\n",
"
44487
\n",
"
Cruzan Dark
\n",
"
12
\n",
"
1000
\n",
"
8.39
\n",
"
12.59
\n",
"
2
\n",
"
25.18
\n",
"
2.00
\n",
"
0.52
\n",
"
0
\n",
"
\n",
"
\n",
"
9
\n",
"
INV-24809400002
\n",
"
2020-01-27
\n",
"
3773
\n",
"
Benz Distributing
\n",
"
501 7th Ave SE
\n",
"
Cedar Rapids
\n",
"
52401.0
\n",
"
POINT (-91.659875 41.97574)
\n",
"
57
\n",
"
LINN
\n",
"
1011800.0
\n",
"
Iowa Distillery Whiskies
\n",
"
125.0
\n",
"
CEDAR RIDGE VINEYARDS LL
\n",
"
928271
\n",
"
Cedar Ridge Private Cask Iowa Bourbon Selectio...
\n",
"
6
\n",
"
750
\n",
"
23.00
\n",
"
34.50
\n",
"
228
\n",
"
7866.00
\n",
"
171.00
\n",
"
45.17
\n",
"
0
\n",
"
\n",
" \n",
"
\n",
"
\n",
" \n",
" \n",
" \n",
"\n",
" \n",
"
\n",
"
\n",
" "
],
"text/plain": [
" invoice_and_item_number date ... volume_sold_gallons over_benjamin\n",
"0 INV-37606800019 2021-06-18 ... 0.52 0\n",
"1 INV-19821600132 2019-06-05 ... 0.31 0\n",
"2 INV-20266400001 2019-06-27 ... 3.56 0\n",
"3 INV-13387900079 2018-07-23 ... 47.55 0\n",
"4 INV-13998900081 2018-08-22 ... 31.70 0\n",
"5 S21846500003 2014-10-20 ... 5.94 0\n",
"6 S26037800001 2015-06-09 ... 97.08 0\n",
"7 INV-06908200123 2017-08-28 ... 1.39 0\n",
"8 INV-20029700068 2019-06-17 ... 0.52 0\n",
"9 INV-24809400002 2020-01-27 ... 45.17 0\n",
"\n",
"[10 rows x 25 columns]"
]
},
"metadata": {},
"execution_count": 7
}
]
},
{
"cell_type": "markdown",
"source": [
"The new column is at the end. I included the `LIMIT` as to reduce computation time for displaying. I won't use that in the subquery here."
],
"metadata": {
"id": "65g3kW3FMCzL"
}
},
{
"cell_type": "code",
"source": [
"%%bigquery --project pic-math\n",
"\n",
"SELECT category_name, SUM(over_benjamin) as number_bottles_over_benjamin\n",
"FROM (SELECT *, (CASE WHEN state_bottle_retail >100 THEN 1 ELSE 0 END) as over_benjamin\n",
" FROM `bigquery-public-data.iowa_liquor_sales.sales`)\n",
"GROUP BY category_name, item_description\n",
"ORDER BY number_bottles_over_benjamin DESC"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 424
},
"id": "EVfN7uOjL_VL",
"outputId": "4604e0ef-7c47-4b77-b0fc-a3f4c9d13deb"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/html": [
"\n",
"
\n",
"
\n",
"
\n",
"\n",
"
\n",
" \n",
"
\n",
"
\n",
"
category_name
\n",
"
number_bottles_over_benjamin
\n",
"
\n",
" \n",
" \n",
"
\n",
"
0
\n",
"
Scotch Whiskies
\n",
"
2552
\n",
"
\n",
"
\n",
"
1
\n",
"
SCOTCH WHISKIES
\n",
"
1897
\n",
"
\n",
"
\n",
"
2
\n",
"
100% Agave Tequila
\n",
"
1579
\n",
"
\n",
"
\n",
"
3
\n",
"
SINGLE MALT SCOTCH
\n",
"
1119
\n",
"
\n",
"
\n",
"
4
\n",
"
Imported Brandies
\n",
"
1090
\n",
"
\n",
"
\n",
"
...
\n",
"
...
\n",
"
...
\n",
"
\n",
"
\n",
"
13825
\n",
"
American Cordials & Liqueur
\n",
"
0
\n",
"
\n",
"
\n",
"
13826
\n",
"
MISC. AMERICAN CORDIALS & LIQUEURS
\n",
"
0
\n",
"
\n",
"
\n",
"
13827
\n",
"
MISC. IMPORTED CORDIALS & LIQUEURS
\n",
"
0
\n",
"
\n",
"
\n",
"
13828
\n",
"
MISC. IMPORTED CORDIALS & LIQUEURS
\n",
"
0
\n",
"
\n",
"
\n",
"
13829
\n",
"
MISC. IMPORTED CORDIALS & LIQUEURS
\n",
"
0
\n",
"
\n",
" \n",
"
\n",
"
13830 rows × 2 columns
\n",
"
\n",
" \n",
" \n",
" \n",
"\n",
" \n",
"
\n",
"
\n",
" "
],
"text/plain": [
" category_name number_bottles_over_benjamin\n",
"0 Scotch Whiskies 2552\n",
"1 SCOTCH WHISKIES 1897\n",
"2 100% Agave Tequila 1579\n",
"3 SINGLE MALT SCOTCH 1119\n",
"4 Imported Brandies 1090\n",
"... ... ...\n",
"13825 American Cordials & Liqueur 0\n",
"13826 MISC. AMERICAN CORDIALS & LIQUEURS 0\n",
"13827 MISC. IMPORTED CORDIALS & LIQUEURS 0\n",
"13828 MISC. IMPORTED CORDIALS & LIQUEURS 0\n",
"13829 MISC. IMPORTED CORDIALS & LIQUEURS 0\n",
"\n",
"[13830 rows x 2 columns]"
]
},
"metadata": {},
"execution_count": 26
}
]
},
{
"cell_type": "markdown",
"source": [
"Well not really sure which is faster but you get to see the `CASE` command too for adding a column. By the way this is sometimes called an indicator variable. When something happens (bottle over $100) you get 1 and 0 otherwise. This is an excellent technique that we will revisist again!"
],
"metadata": {
"id": "UOsHdk98NGi0"
}
},
{
"cell_type": "markdown",
"source": [
"Let's add some more. Let's get the percentage of bottles over a benjamin."
],
"metadata": {
"id": "vV8xq3kcNyR5"
}
},
{
"cell_type": "code",
"source": [
"%%bigquery --project pic-math\n",
"\n",
"SELECT category_name, COUNTIF(state_bottle_retail>100) as number_bottles_over_benjamin, COUNT(*) as number_of_bottles\n",
"FROM `bigquery-public-data.iowa_liquor_sales.sales`\n",
"GROUP BY category_name, item_description\n",
"Order BY number_bottles_over_benjamin DESC"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 424
},
"id": "KnfgAIbuMjd5",
"outputId": "2645b9ee-182e-4eaa-8002-638996b538d5"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/html": [
"\n",
"
\n",
"
\n",
"
\n",
"\n",
"
\n",
" \n",
"
\n",
"
\n",
"
category_name
\n",
"
number_bottles_over_benjamin
\n",
"
number_of_bottles
\n",
"
\n",
" \n",
" \n",
"
\n",
"
0
\n",
"
Scotch Whiskies
\n",
"
2552
\n",
"
2553
\n",
"
\n",
"
\n",
"
1
\n",
"
SCOTCH WHISKIES
\n",
"
1897
\n",
"
1897
\n",
"
\n",
"
\n",
"
2
\n",
"
100% Agave Tequila
\n",
"
1579
\n",
"
3069
\n",
"
\n",
"
\n",
"
3
\n",
"
SINGLE MALT SCOTCH
\n",
"
1119
\n",
"
1119
\n",
"
\n",
"
\n",
"
4
\n",
"
Imported Brandies
\n",
"
1090
\n",
"
1194
\n",
"
\n",
"
\n",
"
...
\n",
"
...
\n",
"
...
\n",
"
...
\n",
"
\n",
"
\n",
"
13825
\n",
"
Straight Bourbon Whiskies
\n",
"
0
\n",
"
2
\n",
"
\n",
"
\n",
"
13826
\n",
"
American Cordials & Liqueur
\n",
"
0
\n",
"
1
\n",
"
\n",
"
\n",
"
13827
\n",
"
PUERTO RICO & VIRGIN ISLANDS RUM
\n",
"
0
\n",
"
1
\n",
"
\n",
"
\n",
"
13828
\n",
"
MISC. IMPORTED CORDIALS & LIQUEURS
\n",
"
0
\n",
"
2
\n",
"
\n",
"
\n",
"
13829
\n",
"
MISC. IMPORTED CORDIALS & LIQUEURS
\n",
"
0
\n",
"
1
\n",
"
\n",
" \n",
"
\n",
"
13830 rows × 3 columns
\n",
"
\n",
" \n",
" \n",
" \n",
"\n",
" \n",
"
\n",
"
\n",
" "
],
"text/plain": [
" category_name ... number_of_bottles\n",
"0 Scotch Whiskies ... 2553\n",
"1 SCOTCH WHISKIES ... 1897\n",
"2 100% Agave Tequila ... 3069\n",
"3 SINGLE MALT SCOTCH ... 1119\n",
"4 Imported Brandies ... 1194\n",
"... ... ... ...\n",
"13825 Straight Bourbon Whiskies ... 2\n",
"13826 American Cordials & Liqueur ... 1\n",
"13827 PUERTO RICO & VIRGIN ISLANDS RUM ... 1\n",
"13828 MISC. IMPORTED CORDIALS & LIQUEURS ... 2\n",
"13829 MISC. IMPORTED CORDIALS & LIQUEURS ... 1\n",
"\n",
"[13830 rows x 3 columns]"
]
},
"metadata": {},
"execution_count": 29
}
]
},
{
"cell_type": "markdown",
"source": [
"Now I cannot just divide those two columns because they don't exist yet!"
],
"metadata": {
"id": "-k0CuiGXOJbV"
}
},
{
"cell_type": "code",
"source": [
"%%bigquery --project pic-math\n",
"\n",
"SELECT category_name, number_bottles_over_benjamin, number_of_bottles, number_bottles_over_benjamin/number_of_bottles as percent_over_benjamin\n",
"FROM (SELECT category_name, COUNTIF(state_bottle_retail>100) as number_bottles_over_benjamin, COUNT(*) as number_of_bottles\n",
" FROM `bigquery-public-data.iowa_liquor_sales.sales`\n",
" GROUP BY category_name, item_description)\n",
"GROUP BY category_name, number_bottles_over_benjamin, number_of_bottles\n",
"ORDER BY percent_over_benjamin DESC"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 424
},
"id": "qCA68V6DOEkE",
"outputId": "3fed081f-6fee-4868-b76a-e9f765ffd601"
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/html": [
"\n",
"
\n",
"
\n",
"
\n",
"\n",
"
\n",
" \n",
"
\n",
"
\n",
"
category_name
\n",
"
number_bottles_over_benjamin
\n",
"
number_of_bottles
\n",
"
percent_over_benjamin
\n",
"
\n",
" \n",
" \n",
"
\n",
"
0
\n",
"
Straight Rye Whiskies
\n",
"
199
\n",
"
199
\n",
"
1.0
\n",
"
\n",
"
\n",
"
1
\n",
"
DECANTERS & SPECIALTY PACKAGES
\n",
"
292
\n",
"
292
\n",
"
1.0
\n",
"
\n",
"
\n",
"
2
\n",
"
Special Order Items
\n",
"
33
\n",
"
33
\n",
"
1.0
\n",
"
\n",
"
\n",
"
3
\n",
"
Special Order Items
\n",
"
24
\n",
"
24
\n",
"
1.0
\n",
"
\n",
"
\n",
"
4
\n",
"
Single Malt Scotch
\n",
"
256
\n",
"
256
\n",
"
1.0
\n",
"
\n",
"
\n",
"
...
\n",
"
...
\n",
"
...
\n",
"
...
\n",
"
...
\n",
"
\n",
"
\n",
"
8349
\n",
"
Temporary & Specialty Packages
\n",
"
0
\n",
"
4
\n",
"
0.0
\n",
"
\n",
"
\n",
"
8350
\n",
"
Flavored Gin
\n",
"
0
\n",
"
2
\n",
"
0.0
\n",
"
\n",
"
\n",
"
8351
\n",
"
CHERRY BRANDIES
\n",
"
0
\n",
"
1
\n",
"
0.0
\n",
"
\n",
"
\n",
"
8352
\n",
"
CREME DE ALMOND
\n",
"
0
\n",
"
1
\n",
"
0.0
\n",
"
\n",
"
\n",
"
8353
\n",
"
Delisted / Special Order Items
\n",
"
0
\n",
"
1
\n",
"
0.0
\n",
"
\n",
" \n",
"
\n",
"
8354 rows × 4 columns
\n",
"
\n",
" \n",
" \n",
" \n",
"\n",
" \n",
"
\n",
"
\n",
" "
],
"text/plain": [
" category_name ... percent_over_benjamin\n",
"0 Straight Rye Whiskies ... 1.0\n",
"1 DECANTERS & SPECIALTY PACKAGES ... 1.0\n",
"2 Special Order Items ... 1.0\n",
"3 Special Order Items ... 1.0\n",
"4 Single Malt Scotch ... 1.0\n",
"... ... ... ...\n",
"8349 Temporary & Specialty Packages ... 0.0\n",
"8350 Flavored Gin ... 0.0\n",
"8351 CHERRY BRANDIES ... 0.0\n",
"8352 CREME DE ALMOND ... 0.0\n",
"8353 Delisted / Special Order Items ... 0.0\n",
"\n",
"[8354 rows x 4 columns]"
]
},
"metadata": {},
"execution_count": 31
}
]
},
{
"cell_type": "markdown",
"source": [
"I got here and realized this is not really the number of bottles. How might you fix that so that this number represents the actual number of bottles sold?"
],
"metadata": {
"id": "2gi-XwJvPV9i"
}
},
{
"cell_type": "markdown",
"source": [
"## Your Turn"
],
"metadata": {
"id": "vmysuVmi4Iko"
}
},
{
"cell_type": "markdown",
"source": [
"Using the dataset 'austin_bikeshare.bikeshare_trips' answer the following questions:\n",
"\n",
"1. What was the second most popular starting station?\n",
"2. How many trips lasted over an hour and were a round trip (started and stopped at the same station)?\n",
"\n",
"\n"
],
"metadata": {
"id": "0vuxVYkn4Tuq"
}
},
{
"cell_type": "markdown",
"source": [
"The following table will be helpful place to start for number 1."
],
"metadata": {
"id": "oswC7uoksvWl"
}
},
{
"cell_type": "code",
"source": [
"%%bigquery --project pic-math\n",
"\n",
"SELECT COUNT(*) as number_trips, start_station_name\n",
"FROM `bigquery-public-data.austin_bikeshare.bikeshare_trips`\n",
"GROUP BY start_station_name\n",
"ORDER BY number_trips DESC"
],
"metadata": {
"id": "kNAcUnBGOup7",
"outputId": "6156f872-1e1b-4ab3-955c-6fe13bda7b54",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 424
}
},
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/html": [
"\n",
"