mql4beginner
mql4beginner

Reputation: 2233

How can I extract the month using sqldf package

I tried to get a view that is based on group by of date by using sqldf package and a month function but I got an error :Error in sqliteSendQuery(con, statement, bind.data) : error in statement: no such function: month

Here is my query: s<-sqldf("select month(dateTime),sum(wolfs) group by dateTime")

Attached is a toy data frame:

 df <- read.table(text = "dateTime         birds    wolfs     snakes
                         2014-05-21        9         7    a
                         2014-04-28        8         4    b
                         2014-04-13        2         8    c
                         2014-03-12        2         3    a
                         2014-02-04        8         3    a
                         2014-02-29        1         2    a
                         2014-01-17        7         1    b
                         2014-01-16        1         5    c
                         2014-09-20        9         7    c
                         2014-08-21        8         7    c ",header = TRUE)

How can I extract the month using sqldf package?

Upvotes: 1

Views: 5882

Answers (1)

nrussell
nrussell

Reputation: 18602

I suspect you are used to SQL Server, but the sqldf backend being used in your case is SQLite, where there is no MONTH function. Try this instead:

R> sqldf("SELECT strftime('%m', dateTime) AS Month
            ,SUM(wolfs) AS Wolves
         FROM df
         GROUP BY strftime('%m', dateTime)")
#    Month Wolves
#  1    01      6
#  2    02      5
#  3    03      3
#  4    04     12
#  5    05      7
#  6    08      7
#  7    09      7

Upvotes: 4

Related Questions