2. Menggunakan DPLYR
chinook<-DBI::dbConnect(RSQLite::SQLite(), "C:/Sqlite/chinook.db")
class(chinook)
## [1] "SQLiteConnection"
## attr(,"package")
## [1] "RSQLite"
RSQLite::dbListTables(chinook)
## [1] "albums" "artists" "customers" "employees"
## [5] "genres" "invoice_items" "invoices" "media_types"
## [9] "playlist_track" "playlists" "sqlite_sequence" "sqlite_stat1"
## [13] "tracks"
dplyr::tbl(chinook, "invoices")
## # Source: table<invoices> [?? x 9]
## # Database: sqlite 3.37.0 [C:\Sqlite\chinook.db]
## InvoiceId CustomerId InvoiceDate BillingAddress BillingCity BillingState
## <int> <int> <chr> <chr> <chr> <chr>
## 1 1 2 2009-01-01 00~ Theodor-Heuss-S~ Stuttgart <NA>
## 2 2 4 2009-01-02 00~ Ullevålsveien 14 Oslo <NA>
## 3 3 8 2009-01-03 00~ Grétrystraat 63 Brussels <NA>
## 4 4 14 2009-01-06 00~ 8210 111 ST NW Edmonton AB
## 5 5 23 2009-01-11 00~ 69 Salem Street Boston MA
## 6 6 37 2009-01-19 00~ Berger Straße 10 Frankfurt <NA>
## 7 7 38 2009-02-01 00~ Barbarossastraß~ Berlin <NA>
## 8 8 40 2009-02-01 00~ 8, Rue Hanovre Paris <NA>
## 9 9 42 2009-02-02 00~ 9, Place Louis ~ Bordeaux <NA>
## 10 10 46 2009-02-03 00~ 3 Chatham Street Dublin Dublin
## # ... with more rows, and 3 more variables: BillingCountry <chr>,
## # BillingPostalCode <chr>, Total <dbl>
invoices <- dplyr::tbl(chinook, "invoices")
class(invoices)
## [1] "tbl_SQLiteConnection" "tbl_dbi" "tbl_sql"
## [4] "tbl_lazy" "tbl"
## # Source: table<invoices> [?? x 9]
## # Database: sqlite 3.37.0 [C:\Sqlite\chinook.db]
## InvoiceId CustomerId InvoiceDate BillingAddress BillingCity BillingState
## <int> <int> <chr> <chr> <chr> <chr>
## 1 1 2 2009-01-01 00~ Theodor-Heuss-S~ Stuttgart <NA>
## 2 2 4 2009-01-02 00~ Ullevålsveien 14 Oslo <NA>
## 3 3 8 2009-01-03 00~ Grétrystraat 63 Brussels <NA>
## 4 4 14 2009-01-06 00~ 8210 111 ST NW Edmonton AB
## 5 5 23 2009-01-11 00~ 69 Salem Street Boston MA
## 6 6 37 2009-01-19 00~ Berger Straße 10 Frankfurt <NA>
## 7 7 38 2009-02-01 00~ Barbarossastraß~ Berlin <NA>
## 8 8 40 2009-02-01 00~ 8, Rue Hanovre Paris <NA>
## 9 9 42 2009-02-02 00~ 9, Place Louis ~ Bordeaux <NA>
## 10 10 46 2009-02-03 00~ 3 Chatham Street Dublin Dublin
## # ... with more rows, and 3 more variables: BillingCountry <chr>,
## # BillingPostalCode <chr>, Total <dbl>
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
q<-invoices %>%
select(InvoiceId, CustomerId, Total) %>%
filter(Total>10)
q
## # Source: lazy query [?? x 3]
## # Database: sqlite 3.37.0 [C:\Sqlite\chinook.db]
## InvoiceId CustomerId Total
## <int> <int> <dbl>
## 1 5 23 13.9
## 2 12 2 13.9
## 3 19 40 13.9
## 4 26 19 13.9
## 5 33 57 13.9
## 6 40 36 13.9
## 7 47 15 13.9
## 8 54 53 13.9
## 9 61 32 13.9
## 10 68 11 13.9
## # ... with more rows
## <SQL>
## SELECT *
## FROM (SELECT `InvoiceId`, `CustomerId`, `Total`
## FROM `invoices`)
## WHERE (`Total` > 10.0)