knitr::opts_chunk$set(
  message = FALSE, 
  warning = FALSE,
  collapse = TRUE,
  comment = "#>",
  fig.path = "README-"
)

sql.helper

The goal of sql.helper is to help building SQL queries in R

Install

# install.packages('devtools')
devtools::install_github("taekyunk/sql.helper")

Example: paren()

This is a basic example of using paren() with SQL IN operator.

id_char <- as.character(1:5)
id_num <- 3:8

# paren() will add single quotes if the field is not numeric
paren(id_char)
paren(id_num)

# to match character with numeric
paren(as.numeric(id_char))

# to match numeric with character
paren(as.character(id_num))

# typical use case will be something like this
query <- paste("select * from tablename where id in", paren(id_num), ";")
print(query)

Example: count_from()

count_from() can be used to build SQL query to count the combination of variables.

library(tidyverse)
c("x", "y") %>% 
    count_from("schema.table")

Example: count_rows()

count_rows() builds SQL query to count the number of observations in a table

count_rows("schema.table")

Example: add_limit()

add_limit(n) adds limit condition at the end of the given SQL query

"select * from schema.table;" %>% add_limit(100)


taekyunk/sql.helper documentation built on May 20, 2019, 1:30 p.m.