sqldf中的DATEPART()

时间:2014-01-24 19:40:05

标签: r sqldf

是否可以在sqldf中使用SQL Server 2008 DATEPART()的典型SQL命令?

我正在扫描文档但没有找到与之相关的任何内容,我对SQLite不熟悉,所以如果我应该这样阅读那么我会

我想做一些简单的事情:

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

修改

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

谢谢,

1 个答案:

答案 0 :(得分:1)

DATEPART(...)是Transact-SQL的一部分,它是Microsoft和Sybase专有的,因此,不,sqldf不支持它。

sqldf的工作原理是根据drv=参数中定义的引擎创建临时数据库。因此,该引擎支持的任何版本的SQL都应该由sqldf支持。有关支持的引擎列表,请参阅文档。如果未指定引擎,则它支持SQLite中定义的词汇表,SQLite是标准SQL语言(documentation here)的子集。

您的示例中的一个问题是您的ORDER_TIME值使用1​​2小时时间格式。 SQLite支持ISO-8601,它需要24小时格式。因此,在您的具体情况下,我能想到的唯一选择是在R中创建HOURS列,并在调用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

如果您的时间数据是24小时格式(强烈推荐,顺便说一下),那么您可以使用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