Is it possible to use the SQL command typical to SQL Server 2008 DATEPART() in sqldf?

I am scanning the documentation but not finding anything related to it, I am unfamiliar with SQLite, so if I should go that way and read then I will

I want to do something simple like:

sqldf("select DISCHARGE_UNIT,
      round(avg(datepart(hour,order_time)),2) `avg order time`
      from data
      group by DISHCHARGE_UNIT)

EDIT

summary(data)


 DISCHARGE.UNIT 
 4SOU   :1295  
 2CAD   :1250  
 2NOR   :1185  
 4NOR   :1144  
 3NOR   :1125  
 3SOU   :1080  
 (Other):2723  
      ORDER.DATE         ORDER.TIME  
 1800-01-01:1213   12:00:00 AM:1213  
 1/4/2013  :  39   11:42:00 AM:  34  
 12/5/2012 :  36   11:51:00 AM:  34  
 2/15/2013 :  35   11:03:00 AM:  32  
 12/19/2012:  33   10:32:00 AM:  29  
 10/25/2012:  31   11:15:00 AM:  29  
 (Other)   :8415   (Other)    :8431 

dput(head(data))
ORDER.TIME = structure(c(734L, 118L, 
    279L, 176L, 268L, 188L), .Label = c("1:00:00 PM", "1:01:00 PM", 
    "1:02:00 PM", "1:03:00 PM", "1:04:00 PM", "1:05:00 PM", "1:06:00 PM", 
    "1:07:00 PM", "1:08:00 PM", "1:09:00 PM", "1:10:00 PM", "1:11:00 PM", 
    "1:12:00 PM", "1:13:00 PM", "1:14:00 PM", "1:15:00 PM", "1:16:00 PM", 
    "1:17:00 PM", "1:18:00 PM", "1:19:00 PM", "1:20:00 PM", "1:21:00 PM", 
    "1:22:00 PM", "1:23:00 PM", "1:24:00 PM", "1:25:00 PM", "1:26:00 PM", 
    "1:27:00 PM", "1:28:00 PM", "1:29:00 PM", "1:30:00 PM", "1:31:00 PM", 
    "1:32:00 PM", "1:33:00 PM", "1:34:00 PM", "1:35:00 PM", "1:36:00 PM", 
    "1:37:00 PM", "1:38:00
ORDER.DATE = structure(c(297L, 352L, 340L, 299L, 400L, 185L
    ), .Label = c("1/1/2013", "1/10/2013", "1/11/2013", "1/12/2013", 
    "1/13/2013", "1/14/2013", "1/15/2013", "1/16/2013", "1/17/2013", 
    "1/18/2013", "1/19/2013", "1/2/2013", "1/20/2013", "1/21/2013", 
    "1/22/2013", "1/23/2013", "1/24/2013", "1/25/2013", "1/26/2013", 
    "1/27/2013", "1/28/2013", "1/29/2013", "1/3/2013", "1/30/2013", 
    "1/31/2013", "1/4/2013", "1/5/2013", "1/6/2013", "1/7/2013", 
    "1/8/2013", "1/9/2013", "10/1/2012", "10/1/2013", "10/10/2012", 
    "1

Thank you,

有帮助吗?

解决方案

DATEPART(...) is part of Transact-SQL which is proprietary to Microsoft and Sybase, so, no, it is not supported in sqldf.

sqldf works by creating a temporary database based on whichever engine is defined in the drv= argument. So whatever version of SQL is supported by that engine, should be supported by sqldf. See the documentation for a list of supported engines. If you don't specify an engine, it supports the vocabulary defined in SQLite, which is a subset of the standard SQL language (documentation here).

One problem in your example is that your ORDER_TIME values use the 12-hour time format. SQLite supports ISO-8601 which requires the 24-hour format. So, in your specific case, the only option I can think of is to create an HOURS column in R and use that in the call to sqldf(...).

library(sqldf)
# create sample data...
set.seed(1)
# ORDER_TIME in 12-hour format; does not conform with ISO-8601
data <- data.frame(ORDER_TIME=strftime(as.POSIXct("1:00:00",format="%H:%M:%S")+seq(12*3600,24*3600,60),format="%I:%M:%S %p"),
                   ORDER.DATE=strftime(as.Date("2013-01-01")+rep(0:30,each=721),format="%m/%d/%Y"),
                   DISCHARGE_UNIT=sample(1:10,721,replace=T))

# add hours column based on ORDER_TIME
data$HOURS <- as.numeric(strftime(as.POSIXct(data$ORDER_TIME,format="%I:%M:%S %p"),format="%H"))
sqldf("select DISCHARGE_UNIT,
      round(avg(HOURS),2) `avg order time`
      from data
      group by DISCHARGE_UNIT")
#    DISCHARGE_UNIT avg order time
# 1               1          16.91
# 2               2          16.69
# 3               3          16.64
# 4               4          17.10
# 5               5          15.78
# 6               6          15.01
# 7               7          17.09
# 8               8          15.12
# 9               9          17.68
# 10             10          17.17

If your time data was in 24-hour format (highly recommended, by the way), then you could use the SQLite date and time functions:

set.seed(1)
# ORDER_TIME in 24-hour format; conforms to ISO-8601
data <- data.frame(ORDER_TIME=strftime(as.POSIXct("1:00:00",format="%H:%M:%S")+seq(12*3600,24*3600,60),format="%H:%M:%S"),
                   ORDER.DATE=strftime(as.Date("2013-01-01")+rep(0:30,each=721),format="%m/%d/%Y"),
                   DISCHARGE_UNIT=sample(1:10,721,replace=T))
sqldf("select DISCHARGE_UNIT,
      round(avg(strftime('%H',ORDER_TIME)),2) `avg order time`
      from data
      group by DISCHARGE_UNIT",drv="SQLite")
#    DISCHARGE_UNIT avg order time
# 1               1          16.91
# 2               2          16.69
# 3               3          16.64
# 4               4          17.10
# 5               5          15.78
# 6               6          15.01
# 7               7          17.09
# 8               8          15.12
# 9               9          17.68
# 10             10          17.17
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top