I have a two tables (Table1 and Table2):
Table1
Date Name Other
2014-02-08 Alex 1
2014-06-15 Bob 1
Table2
Date Name Count
2014-02-07 Alex 1
2014-01-31 Alex 2
2014-02-09 Alex 4
2014-02-08 Alex 10
2014-02-10 Alex 0
2014-02-01 Alex 4
2014-01-08 Alex 5
2014-03-08 Alex 4
2014-06-01 Bob 22
2014-06-02 Bob 0
2014-06-10 Bob 9
2014-06-15 Bob 3
2014-06-16 Bob 3
2014-06-20 Bob 5
2014-06-14 Bob 18
2014-07-11 Bob 1
2014-08-15 Bob 2
I am having a difficult time constructing a query that accomplishes the following:
- From Table1, run through each "Date" and "Name"
- For a given "Date" and "Name" in Table1, go through Table2 and grab all rows that also have the same "Name" and that have dates that are 10 days before the "Date" (in Table1) and 5 days after "Date" (in Table1).
So, for Table1, "Alex" on "2014-02-08", I want to grab all rows in Table2 that also say "Alex" but whose date is between "2014-01-29" (10 days before 2014-02-08) and "2014-02-13" (5 days after 2014-02-08).
For "Bob" on "2014-06-15", I want to grab all rows in Table2 that also say "Bob" but whose date is between "2014-06-05" (10 days before 2014-06-15) and "2014-06-20" (5 days after 2014-06-15).
The expected output is:
Date Name Count
2014-02-07 Alex 1
2014-01-31 Alex 2
2014-02-09 Alex 4
2014-02-08 Alex 10
2014-02-10 Alex 0
2014-02-01 Alex 4
2014-06-10 Bob 9
2014-06-15 Bob 3
2014-06-16 Bob 3
2014-06-20 Bob 5
2014-06-14 Bob 18
In my real work, the number of rows in Table1 is much larger and the number of days I'd like to grab before/after the reference date can vary.