Question -
how to use apply function or for loop to run this query below over different values
run query
dbgetQuery(conn, " SELECT ID , Name , Date , Product
FROM table xyz
where Year = i ")
where i is years ( 2010 to 2016 )
how to get table.2011 = data for 2011( IDs , Names , Dates , Product) table.2012 = data for 2012( IDs , Names , Dates , Product) and so on..for each year.
dbGetQueryshould return a data.frame, the methodology will be the same. (This is a strong suggestion against havingtable.2011andtable.2012, instead going for alistof data.frames.)sapply(2010:2016, function(i) dbGetQuery(conn, "...")). You should not be inserting the value directly into the query string, instead you should use a parameterized queries. Unfortunately, all databases do it differently;RPostgresuses$1,RSQLiteuses:varname,RSQLServeruses?1, etc. You said "SQL Db" which does nothing to narrow it down. "What type of database are you using?" (continued ...)