请参阅附件以供参考。假设我有一个表library(data.table)
dt <- fread("id boro block date end_date
1 1 1 01/01/1991 01/01/1992
1 1 2 01/01/1991 01/01/1992
1 2 3 01/01/1991 01/01/1992
1 2 4 01/01/1991 NA
2 1 1 01/01/1992 01/01/1993
2 1 2 01/01/1992 01/01/1993
2 2 3 01/01/1992 NA
2 2 5 01/01/1992 NA
3 1 1 01/01/1993 NA
3 1 2 01/01/1993 NA
3 2 6 01/01/1993 NA
3 2 7 01/01/1993 NA")
,其中包含以下字段CARRIER_RESPONSE
,Carrier
,Shipment
,Acceptance
我想获得每个承运人每天的累计接受计数。
到目前为止,我来到了下面的代码:
Day
这段代码为我提供了接受响应= A的行的正确运行计数,但是在接受响应= D的行上,我也想显示接受响应= A的运行计数。
基本上,我想在每行上指示承运人今天已经接受了多少次托运。这可能吗?
谢谢
阿米尔
运营商响应:
答案 0 :(得分:0)
一种实现方式-将sum与Analytics子句和窗口一起使用。
SELECT ColumnB
FROM Table2 WHERE
ColumnB IN (SELECT ColumnA FROM Table1)
答案 1 :(得分:0)
我想你想要
SELECT Carrier, Shipment, Acceptance, Day,
SUM(CASE WHEN Acceptance = 'A' THEN 1 ELSE 0 END) OVER
(PARTITION BY Carrier
ORDER BY Day
) AS "Cumulative Count of Acceptance per Day"
FROM CARRIER_RESPONSE;
如果Acceptance
仅采用值'A'
和NULL
,则可以将其缩短为:
SELECT Carrier, Shipment, Acceptance, Day,
COUNT(Acceptance) OVER
(PARTITION BY Carrier
ORDER BY Day
) AS "Cumulative Count of Acceptance per Day"
FROM CARRIER_RESPONSE;