Fixed R code to run with new SQL.