-
Notifications
You must be signed in to change notification settings - Fork 0
/
PM 566 Lab 10.qmd
277 lines (215 loc) · 6 KB
/
PM 566 Lab 10.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
---
title: "PM 566 Lab 10"
author: "Dana Gonzalez"
format: html
editor: visual
embed-resources: true
theme: cosmo
---
# Setup
```{r, results='hide'}
# Install and Load Libraries
library(RSQLite)
library(DBI)
library(DT)
library(dplyr)
library(tidyverse)
# Initialize a Temporary in Memory Database
con <- dbConnect(SQLite(), ":memory:")
# Download Tables
actor <- read.csv("https://raw.githubusercontent.com/ivanceras/sakila/master/csv-sakila-db/actor.csv")
rental <- read.csv("https://raw.githubusercontent.com/ivanceras/sakila/master/csv-sakila-db/rental.csv")
customer <- read.csv("https://raw.githubusercontent.com/ivanceras/sakila/master/csv-sakila-db/customer.csv")
payment <- read.csv("https://raw.githubusercontent.com/ivanceras/sakila/master/csv-sakila-db/payment_p2007_01.csv")
# Copy data.frames to Database
dbWriteTable(con, "actor", actor)
dbWriteTable(con, "rental", rental)
dbWriteTable(con, "customer", customer)
dbWriteTable(con, "payment", payment)
```
# Exercise 1
```{r}
# Order Actor Names by Last, Then First Names
query <-
"SELECT actor_id, first_name, last_name
FROM actor
ORDER BY last_name, first_name"
first_last <- dbGetQuery(con, query)
datatable(first_last,
class = 'cell-border stripe',
options = list(pageLength = 10,
lengthMenu = c(5, 10, 20, 50),
searching = TRUE,
ordering = TRUE))
```
# Excercise 2
```{r}
# Retrieve the ID, First, and Last Names for Actors Whose Last Name Equals ‘WILLIAMS’ or ‘DAVIS’
query2 <-
"SELECT actor_id, first_name, last_name
FROM actor
WHERE last_name IN ('WILLIAMS', 'DAVIS')"
williams_davis <- dbGetQuery(con, query2)
williams_davis
```
# Excercise 3
```{r}
# Select ID's For Customers Who Rented a Film on July 5, 2005
query3 <-
"SELECT DISTINCT customer_id
FROM rental
WHERE DATE(rental_date) = '2005-07-05'"
july_rental <- dbGetQuery(con, query3)
july_rental
```
# Exercise 4
### Excercise 4.1
```{r}
# Retrieve Rows From Payment Table Where Values Equal 1.99, 7.99, or 9.99
query4 <-
"SELECT payment_id, customer_id, staff_id, rental_id, amount, payment_date
FROM payment
WHERE amount IN (1.99, 7.99, 9.99)"
payments <- dbGetQuery(con, query4)
datatable(payments,
class = 'cell-border stripe',
options = list(pageLength = 10,
lengthMenu = c(5, 10, 20, 50),
searching = TRUE,
ordering = TRUE))
```
### Excercise 4.2
```{r}
# Retrieve Rows From Payment Table Where Values Are Greater Than 5
query5 <-
"SELECT payment_id, customer_id, staff_id, rental_id, amount, payment_date
FROM payment
WHERE amount > 5"
payments2 <- dbGetQuery(con, query5)
datatable(payments2,
class = 'cell-border stripe',
options = list(pageLength = 10,
lengthMenu = c(5, 10, 20, 50),
searching = TRUE,
ordering = TRUE))
```
# Exercise 5
```{r}
# Retrieve Payment IDs and Amounts From Customers Whose Last Name is ‘DAVIS’
query6 <-
"SELECT payment_id, amount
FROM customer
INNER JOIN payment ON customer.customer_id = payment.customer_id
WHERE last_name = 'DAVIS'"
davis <- dbGetQuery(con, query6)
davis
```
# Exercise 6
### Excercise 6.1
```{r}
# Count Number of Rows in Rental Table
query7 <-
"SELECT COUNT(*) row
FROM rental"
row_count <- dbGetQuery(con, query7)
row_count
```
### Excercise 6.2
```{r}
# Count Number of Rentals for Each Customer
query8 <-
"SELECT customer_id,
COUNT (*) as count
FROM rental
GROUP BY customer_id"
rentals_per_customer <- dbGetQuery(con, query8)
datatable(rentals_per_customer,
class = 'cell-border stripe',
options = list(pageLength = 10,
lengthMenu = c(5, 10, 20, 50),
searching = TRUE,
ordering = TRUE))
```
### Excercise 6.3
```{r}
# Count Number of Rentals for Each Customer in Descending Order
query9 <-
"SELECT customer_id,
COUNT (*) as count
FROM rental
GROUP BY customer_id
ORDER BY count DESC"
rentals_descending <- dbGetQuery(con, query9)
datatable(rentals_descending,
class = 'cell-border stripe',
options = list(pageLength = 10,
lengthMenu = c(5, 10, 20, 50),
searching = TRUE,
ordering = TRUE))
```
### Excercise 6.4
```{r}
# Count Number of Rentals for Each Customer With At Least 40 Rentals
query10 <-
"SELECT customer_id,
COUNT (*) as count
FROM rental
GROUP BY customer_id
HAVING COUNT(*) >= 40
ORDER BY count DESC"
rentals_40 <- dbGetQuery(con, query10)
rentals_40
```
# Excercise 7
### Excercise 7.1
```{r}
# Summary Statistics for Payment Table
query11 <-
"SELECT
MAX(amount) AS max_payment,
MIN(amount) AS min_payment,
AVG(amount) AS avg_payment,
SUM(amount) AS total_payment
FROM payment"
payment_summary <- dbGetQuery(con, query11)
payment_summary
```
### Excercise 7.2
```{r}
# Summary Statistics for Payment Table by Customer
query12 <-
"SELECT customer_id,
MAX(amount) AS max_payment,
MIN(amount) AS min_payment,
AVG(amount) AS avg_payment,
SUM(amount) AS total_payment
FROM payment
GROUP BY customer_id"
summary_customer <- dbGetQuery(con, query12)
datatable(summary_customer,
class = 'cell-border stripe',
options = list(pageLength = 10,
lengthMenu = c(5, 10, 20, 50),
searching = TRUE,
ordering = TRUE))
```
### Excercise 7.3
```{r}
# Summary Statistics for Payment Table by Customers with More Than 5 Payments
query13 <-
"SELECT customer_id,
MAX(amount) AS max_payment,
MIN(amount) AS min_payment,
AVG(amount) AS avg_payment,
SUM(amount) AS total_payment
FROM payment
GROUP BY customer_id
HAVING COUNT(payment_id) > 5"
summary_customer_5 <- dbGetQuery(con, query13)
summary_customer_5
```
# Cleanup
```{r}
# Cleanup!
dbDisconnect(con)
```