使用sqldf根据多个条件进行计数

时间:2020-02-06 13:41:52

标签: sql r sqldf

大家好,我正在使用sqldf在R上编写sql查询,但似乎遇到了障碍。我有一个带有ID列,两个日期列和一个按列分组的表。

AlertDate  AppointmentDate  ID  Branch
01/01/20   04/01/20         1   W1
01/01/20   09/01/20         1   W1
08/01/20   09/01/20         1   W2
01/01/20   23/01/20         1   W1

我正在写的查询是

sqldf('select Branch,count(ID) from df where AlertDate <= AppointmentDate 
and AppointmentDate <AlertDate+7 group by Branch')

从该查询中我得到的结果是

Branch Count
W1      1
W2      1

根据查询正确的位置。我要实现的是第二个条件为假,即AppointmentDate小于AlertDate + 7。与其删除计数,不如根据日期将其计入下一组。例如,如果警报日期为01/01/20且约会日期为23/01/20,则应将其计入W4。 ceil((Appointmentdate-alertdate)/ 7)所以最后我希望结果为

Branch  Count
W1      1
W2      2
W4      1

第二行应计入W2,第四行应计入W4,而不是被丢弃。我试图在R中使用sqldf在sql中实现这一目标。任何使用R或Sql的可能解决方案都对我有用。

输出dput(测试)

structure(list(AlertDate = structure(c(18262, 18262, 18269, 18262), class = "Date"), AppointmentDate = structure(c(18265, 18270,18270, 18284), class = 
"Date"), ID = c(1, 1, 1, 1), Branch = c("W1","W1", "W2", "W1")), class = c("spec_tbl_df", "tbl_df", "tbl","data.frame"), row.names = c(NA, -4L), problems = 
structure(list( row = 4L, col = "Branch", expected = "", actual = "embedded null", 
file = "'C:/Users/FRssarin/Desktop/test.txt'"), row.names = c(NA,-1L), class = c("tbl_df", "tbl", "data.frame")), spec = structure(list(  cols = list(AlertDate = 
structure(list(format = "%d/%m/%y"), class = c("collector_date", 
"collector")), AppointmentDate = structure(list(format = "%d/%m/%y"), class = c("collector_date",  "collector")), ID = structure(list(), class = c("collector_double", "collector")), Branch = structure(list(), class = 
c("collector_character",  "collector"))), default = structure(list(), class = c("collector_guess",  "collector")), skip = 1), class = "col_spec"))

enter image description here

1 个答案:

答案 0 :(得分:1)

这是使用data.table的一种方法

df <- structure(list(AlertDate = structure(c(18262, 18262, 18269, 18262), class = "Date"), AppointmentDate = structure(c(18265, 18270,18270, 18284), class = 
                                                                                                                     "Date"), ID = c(1, 1, 1, 1), Branch = c("W1","W1", "W2", "W1")), class = c("spec_tbl_df", "tbl_df", "tbl","data.frame"), row.names = c(NA, -4L), problems = 
              structure(list( row = 4L, col = "Branch", expected = "", actual = "embedded null", 
                              file = "'C:/Users/FRssarin/Desktop/test.txt'"), row.names = c(NA,-1L), class = c("tbl_df", "tbl", "data.frame")), spec = structure(list(  cols = list(AlertDate = 
                                                                                                                                                                                      structure(list(format = "%d/%m/%y"), class = c("collector_date", 

我正在将其转换为data.table并为您的逻辑创建一个新列。

library(data.table)
df <- data.table(df)
df <- df[AlertDate <= AppointmentDate] 
df[, new_branch:= ifelse(as.numeric(AppointmentDate-AlertDate)>=7
            ,paste0("W", as.character(ceiling(as.numeric(AppointmentDate-AlertDate)/7))),Branch)]

这是结果表

    AlertDate AppointmentDate ID Branch new_branch
1: 2020-01-01      2020-01-04  1     W1         W1
2: 2020-01-01      2020-01-09  1     W1         W2
3: 2020-01-08      2020-01-09  1     W2         W2
4: 2020-01-01      2020-01-23  1     W1         W4

这是愚蠢的结果。

df[, .(.N, alert=head(AlertDate,1),  appoint=head(AppointmentDate,1)), by = list(new_branch)]
   new_branch N      alert    appoint
1:         W1 1 2020-01-01 2020-01-04
2:         W2 2 2020-01-01 2020-01-09
3:         W4 1 2020-01-01 2020-01-23