I'm doing an analysis using SQL and R, I want to join two tables like below listed:
Table 1:
ID date
a11 20150302
a11 20150302
a22 20150303
a22 20150304
a33 20150306
a44 20150306
a55 20150307
a66 20150308
a66 20150309
a66 20150310
Table 2
ID date
a11 20150303
a22 20150304
a22 20150305
a44 20150306
a66 20150308
a66 20150310
The situation is like: Customer got called (table1) and Customer called back for more info (table two)
So what I want to do in the analysis is to:
- Only show IDs that in both table.
- Match table 2 date to table 1 date by:
- Match the closest date
- table 2 date must >= table 1 date (like the example in result "a66" 20150310 is assigned to table1 date 20150310, while 20150308 is assigned to 20150308, not 20150309)
Result:
ID table1 date table2 date
a11 20150302
a11 20150302 20150303
a22 20150303 20150304
a22 20150304 20150305
a44 20150306 20150306
a66 20150308 20150308
a66 20150309
a66 20150310 20150310
Is there any solutions for this many to many (but I don't want the n*m as result, I want 1 to 1) matching/join? Solution either in R or SQL will be wanted.
Thanks