我有一張訂單表和一張賬戶活動表。我正在尋找該訂單上最近的帳戶活動。我想遍歷每個訂單來查找與該賬戶相匹配的活動,以及與訂單日期最近的日期。根據其他條件按日期查找最新記錄
rec type date account nearest.rec
1 Order 12/1/2016 A
2 Order 11/14/2016 B
3 Activity 11/13/2016 A
4 Activity 10/15/2016 C
5 Order 11/13/2016 C
6 Activity 11/16/2016 A
7 Activity 11/17/2016 A
8 Activity 10/14/2016 B
9 Activity 11/4/2016 B
想將其轉換成這樣:
rec type date account nearest.rec.actv
1 Order 12/1/2016 A 7
2 Order 11/14/2016 B 9
3 Activity 11/13/2016 A
4 Activity 10/15/2016 C
5 Order 11/13/2016 C 4
6 Activity 11/16/2016 A
7 Activity 11/17/2016 A
8 Activity 10/14/2016 B
9 Activity 11/4/2016 B
或者變成自己的數據幀
rec type date account nearest.rec.actv actv.date
1 Order 12/1/2016 A 7 11/17/2016
2 Order 11/14/2016 B 9 11/4/2016
5 Order 11/13/2016 C 4 10/15/2016
@nrussell的感謝!當它出錯時,我只是試圖修復它。 –