是否可以在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


谢谢,

最佳答案

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

sqldf通过基于drv=参数中定义的任何引擎创建临时数据库来工作。因此,该引擎支持的任何SQL版本都应由sqldf支持。请参阅文档以获取受支持引擎的列表。如果未指定引擎,则它支持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

关于r - sqldf中的DATEPART(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21341089/

10-12 17:43