So basically I was wondering how I can join two tables on the dates if they are different. The first table is my main tables which includes all my customers that have bought items. The date of purchase is a single point in the past:
Customers
+--------+----------+-------+------------+
| custid | Quantity | Price | ReportDate |
+--------+----------+-------+------------+
| 371965 | 12 | 2 | 9/1/2016 |
| 371965 | 2 | 5 | 2/25/2018 |
| 377958 | 45 | 3 | 9/1/2016 |
| 270723 | 12 | 1.25 | 5/1/2014 |
| 270723 | 10.86 | 1.25 | 6/1/2014 |
| 270723 | 12.29 | 1.3 | 7/1/2014 |
| 270723 | 12.29 | 1.4 | 9/15/2016 |
+--------+----------+-------+------------+
So I would like to join my customers tables on the table with the discounts. The time when the discount was in effect is basically an entire period until a new discount was issued:
Discounts
+----+-----------+----------+
| id | startdate | discount |
+----+-----------+----------+
| 1 | 7/18/2013 | 0.1 |
| 2 | 1/10/2014 | 0.25 |
| 3 | 7/11/2016 | 0.11 |
| 4 | 9/14/2016 | 0.12 |
| 5 | 1/12/2017 | 0.15 |
| 6 | 2/6/2017 | 0.22 |
| 7 | 6/28/2017 | 0.09 |
+----+-----------+----------+
So my goal is to link both tables and see which purchase date falls in the appropriate interval of the discounts. This would be my goal:
+--------+----------+-------+------------+----+-----------+----------+
| custid | Quantity | Price | ReportDate | id | startdate | discount |
+--------+----------+-------+------------+----+-----------+----------+
| 371965 | 12 | 2 | 9/1/2016 | 3 | 7/11/2016 | 0.11 |
| 371965 | 2 | 5 | 2/25/2018 | 7 | 6/28/2017 | 0.09 |
| 377958 | 45 | 3 | 9/1/2016 | 3 | 7/11/2016 | 0.11 |
| 270723 | 12 | 1.25 | 5/1/2014 | 2 | 41649 | 0.25 |
| 270723 | 10.86 | 1.25 | 6/1/2014 | 2 | 1/10/2014 | 0.25 |
| 270723 | 12.29 | 1.3 | 7/1/2014 | 2 | 1/10/2014 | 0.25 |
| 270723 | 12.29 | 1.4 | 9/15/2016 | 4 | 9/14/2016 | 0.12 |
+--------+----------+-------+------------+----+-----------+----------+