我有两个数据表,让我们称它们weights
为values
.
该weights
表有 5 列,如下所示:
first POSIXct
late POSIXct
nodeid integer
aggid integer
weight numeric
该values
表有这些列
nodeid integer
Date POSIXct
hour integer
value decimal
这个想法是生成一个新表,它将根据权重将节点的加权平均值转换为聚合节点。但是,权重会随着时间而变化,需要根据第一个和最晚的日期进行匹配。执行此操作的 SQL 语法看起来像这样
select v.Date, v.hour, w.aggid, sum(v.value*w.weight) as aggvalue
from values v inner join weights w
on v.nodeid=w.nodeid and v.date between w.first and w.late
group by aggid, date, hour
between
鉴于SQL 语法中的逻辑,我不确定从哪里开始。这在 data.table 语法中是否可行,或者我需要将weights
表转换为每天都有一行而不是使用范围?
这是一些示例数据(对不起,它太长了)......
values<-data.table(nodeid = c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L,
6L, 6L, 6L, 6L, 6L), Date = c("2013-07-02", "2013-07-02", "2013-07-05",
"2013-07-08", "2013-07-10", "2013-07-02", "2013-07-02", "2013-07-05",
"2013-07-08", "2013-07-10", "2013-07-02", "2013-07-02", "2013-07-05",
"2013-07-08", "2013-07-10", "2013-07-02", "2013-07-02", "2013-07-05",
"2013-07-08", "2013-07-10", "2013-07-02", "2013-07-02", "2013-07-05",
"2013-07-08", "2013-07-10", "2013-07-02", "2013-07-02", "2013-07-05",
"2013-07-08", "2013-07-10"), hour = c(1L, 2L, 23L, 2L, 2L, 1L,
2L, 23L, 2L, 2L, 1L, 2L, 23L, 2L, 2L, 1L, 2L, 23L, 2L, 2L, 1L,
2L, 23L, 2L, 2L, 1L, 2L, 23L, 2L, 2L), value = c(8.234, 3.218,
0.787, 8.689, 6.218, 6.89, 1.914, 2.459, 6.683, 8.122, 0.281,
1.136, 1.993, 7.27, 9.582, 5.777, 1.375, 9.204, 7.862, 0.633,
2.433, 1.842, 7.178, 10.692, 1.417, 1.259, 2.619, 0.031, 6.744,
5.941))
weights<-data.table(first = c("2013-07-01", "2013-07-01", "2013-07-01",
"2013-07-01", "2013-07-01", "2013-07-01", "2013-07-08", "2013-07-08",
"2013-07-08", "2013-07-08", "2013-07-08", "2013-07-08"), late = c("2013-07-07",
"2013-07-07", "2013-07-07", "2013-07-07", "2013-07-07", "2013-07-07",
"2013-07-20", "2013-07-20", "2013-07-20", "2013-07-20", "2013-07-20",
"2013-07-20"), nodeid = c(1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L,
4L, 5L, 6L), aggid = c(1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L,
2L, 2L), weight = c(0.5, 0.25, 0.25, 0.3, 0.5, 0.2, 0.6, 0.2,
0.2, 0.4, 0.45, 0.15))
exresults<-data.table(aggid = c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L), Date = c("2013-07-02", "2013-07-02", "2013-07-02", "2013-07-02",
"2013-07-05", "2013-07-05", "2013-07-08", "2013-07-08", "2013-07-10",
"2013-07-10"), hour = c(1L, 1L, 2L, 2L, 23L, 23L, 2L, 2L, 2L,
2L), aggvalue = c(5.90975, 3.2014, 2.3715, 1.8573, 1.5065, 6.3564,
8.004, 8.9678, 7.2716, 1.782))