I am attempting to extract the closest value based on date between 2 tables.
Table 1 looks like:
id | value | date |
---|---|---|
1 | aa | 10/2/21 |
2 | ab | 4/6/18 |
Table 2 looks like:
id | value_2 | date_2 |
---|---|---|
1 | ba | 4/12/11 |
1 | bb | 8/16/08 |
1 | bc | 10/4/21 |
2 | bd | 4/26/18 |
2 | bd | 3/26/19 |
I would like the output table to return value_2
that corresponds to the same id in both tables based on the closest date. ex- id 1
should return value bc
since 10/4/21 is closest to 10/2/21
So the output table should mutate this value_2
to look like-
id | value | date | value_2 |
---|---|---|---|
1 | aa | 10/2/21 | bc |
2 | ab | 4/6/18 | bd |