我是SQL服务器的新手,我正在尝试添加基于条件的先前数据。
这是迄今为止我从2本书中获得的数据的一个例子。
LATEST6.0b1
rollerOLS <- function(data, measure, predict, predictor){
error <- c()
m <- dim(data)[1]
for(i in 1:(floor(m/142)-10)){
data.new <- as.data.frame(data[c((1+(142*(i-1))):((i+9)*142)),])
data.pred <- as.data.frame(data[c((1+(142*(i+9))):((i+10)*142)-1),])
n <- dim(data.new)[1]
k <- dim(data.pred)[1]
x <- data.new[-1,predictor]
y <- data.new[-n, predict]
mod <- lm(y ~ x)
ts <- predict.lm(mod, newdata = data.frame(data.pred[, predictor]), interval="none")
actual <- data.pred[-k,predict]
error[i] <- measure(ts, actual)
}
return(mod)
}
&#13;
LATEST6.0b2
EQOF_CNTR_N STATUS_C LEN_Q ABBR_VESSEL_M
AHLU1100548 F 20 p
ALQU7004370 F 20 f
AMFU8768914 F 40 g
AMFU8786693 F 40 s
AMFU8832490 F 40 y
AMFU8837379 F 40 g
AMFU8847021 F 40 d
ATB_DT SNAP_DT CT STATUS_C
23/3/2017 19:45 23/3/2017 12:00 B E
26/3/2017 8:15 23/3/2017 12:00 K E
23/3/2017 10:15 23/3/2017 12:00 R F
23/3/2017 3:50 23/3/2017 12:00 V F
25/3/2017 19:50 23/3/2017 12:00 T F
26/3/2017 17:00 23/3/2017 12:00 P F
23/3/2017 5:50 23/3/2017 12:00 G F
&#13;
这是我到目前为止运行的代码。
EQOF_CNTR_N STATUS_C LEN_Q ABBR_VESSEL_M
AHLU1100548 F 20 p
ALQU7004370 F 20 f
AMFU8768914 F 40 g
AMFU8786693 F 40 s
AMFU8832490 F 40 y
AMFU8837379 F 40 g
AMFU8847021 F 40 d
ATB_DT SNAP_DT CT STATUS_C
23/3/2017 19:45 23/3/2017 12:00 B E
26/3/2017 8:15 23/3/2017 12:00 K E
23/3/2017 10:15 23/3/2017 12:00 R F
23/3/2017 3:50 23/3/2017 12:00 V F
25/3/2017 19:50 23/3/2017 12:00 T F
26/3/2017 17:00 23/3/2017 12:00 P F
23/3/2017 5:50 23/3/2017 12:00 G F
ARRIVAL_TIME
22/3/2017 18:17
24/3/2017 14:45
22/3/2017 12:11
22/3/2017 4:00
24/3/2017 16:45
24/3/2017 9:58
22/3/2017 15:16
&#13;
这是我得到的输出
SELECT a.SNAP_DT, a.VOYAGE_OUT_N,a.ABBR_VESSEL_M, a.STATUS_C,a.CT,
(SELECT count(*) FROM [LATEST6.0b1] y, [LATEST6.0b2] z WHERE ARRIVAL_TIME <= SNAP_DT AND y.EQOF_CNTR_N = z.EQOF_CNTR_N AND y.SNAP_DT = a.SNAP_DT AND y.VOYAGE_OUT_N = a.VOYAGE_OUT_N AND y.STATUS_C = a.STATUS_C AND y.ABBR_VESSEL_M = a.ABBR_VESSEL_M AND y.CT = a.CT) c,
(SELECT count(*) FROM [LATEST6.0b1] y, [LATEST6.0b2] z WHERE ARRIVAL_TIME <= SNAP_DT AND y.EQOF_CNTR_N = z.EQOF_CNTR_N AND y.VOYAGE_OUT_N = a.VOYAGE_OUT_N AND y.STATUS_C = a.STATUS_C AND y.ABBR_VESSEL_M = a.ABBR_VESSEL_M AND y.CT = a.CT) totalc
from
[LATEST6.0b1] a,
[LATEST6.0b2] b
WHERE
a.EQOF_CNTR_N = b.EQOF_CNTR_N
GROUP BY
a.SNAP_DT,
a.VOYAGE_OUT_N,
a.ABBR_VESSEL_M,
a.STATUS_C,
a.CT
ORDER BY
a.CT,
a.STATUS_C,
a.VOYAGE_OUT_N,
a.SNAP_DT asc;
&#13;
如果查看c和totalc列,我会尝试根据相同的VOYAGE_OUT_N和ABBR_VESSEL_M将数字添加到上一行。因此,所需的输出应如下所示。
SNAP_DT VOYAGE_OUT_N ABBR_VESSEL_M
2017-03-31 20:00:00 0006W P
2017-04-01 00:00:00 0006W P
2017-04-01 04:00:00 0006W P
2017-04-07 20:00:00 0007S Q
2017-04-08 00:00:00 0007S Q
2017-04-10 16:00:00 0007S Q
c totalc
12 36
12 36
12 36
0 126
0 126
126 126
&#13;
你有什么想法吗?提前谢谢。