Connecting Tech Pros Worldwide Forums | Help | Site Map

Linking Tables by Date Range

Member
 
Join Date: Sep 2007
Posts: 94
#1: Jun 13 '08
I have a table with a date stamp on it. [tblDistLearnTrack]
Expand|Select|Wrap|Line Numbers
  1. unique_id      dist_created_date
  2. 1              01/06/2008
  3. 2              01/06/2008
  4. 3              05/06/2008
  5. 4              22/06/2008
And a table with date ranges on it. [tblPayMonths]
Expand|Select|Wrap|Line Numbers
  1. month_id       start_date     end_date      pay_month
  2. 07-APRMAY08    20/05/2008     19/06/2008    JUN-08 
  3. 07-MAYJUN08    20/06/2008     19/07/2008    JUL-08
  4. 07-JUNJUL08    20/07/2008     19/08/2008    AUG-08
What i want to do is join the two tables together and pull in the relevant pay month where the created date falls in between the date ranges.
So i am hoping to see results like this:
Expand|Select|Wrap|Line Numbers
  1. unique_id      dist_created_date      pay_month
  2. 1              01/06/2008             JUN-08
  3. 2              01/06/2008             JUN-08
  4. 3              05/06/2008             JUN-08
  5. 4              22/06/2008             JUL-08
Does anyone have any ideas?

Moderator
 
Join Date: Feb 2008
Location: Beauly, near Inverness, Scotland
Posts: 1,576
#2: Jun 13 '08

re: Linking Tables by Date Range


One simple way to do this is to use a WHERE clause to select the relevant rows from the two otherwise-unjoined tables:

Expand|Select|Wrap|Line Numbers
  1. SELECT tblDistLearnTrack.unique_id, tblDistLearnTrack.dist_created_date, tblPayMonths.pay_month
  2. FROM tblDistLearnTrack, tblPayMonths
  3. WHERE (((tblDistLearnTrack.dist_created_date) Between [start_date] And [end_date]));
which on your test data plus a couple of rows results in:

Expand|Select|Wrap|Line Numbers
  1. unique_id...dist_created_date......pay_month
  2. 1..............01/06/2008............JUN-08
  3. 2..............01/06/2008............JUN-08
  4. 3..............05/06/2008............JUN-08
  5. 4..............22/06/2008............JUL-08
  6. 5..............30/07/2008............AUG-08
  7. 6..............01/07/2008............JUL-08
-Stewart
Member
 
Join Date: Sep 2007
Posts: 94
#3: Jun 13 '08

re: Linking Tables by Date Range


Thanks Stewart. Knew it would be easy!
Member
 
Join Date: Sep 2007
Posts: 94
#4: Jun 13 '08

re: Linking Tables by Date Range


Hang on a minute, that isn't working. It is omitting records....will post an example....
Member
 
Join Date: Sep 2007
Posts: 94
#5: Jun 13 '08

re: Linking Tables by Date Range


My apologies, it did work!
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,730
#6: Jun 16 '08

re: Linking Tables by Date Range


I would recommend using INNER JOIN within the FROM clause rather than the WHERE clause. This isn't critical, but it's clearer (for me at least) what is being done (SQL isn't easy to read at the best of times).
Expand|Select|Wrap|Line Numbers
  1. SELECT tDLT.unique_id,
  2.        tDLT.dist_created_date,
  3.        tPM.pay_month
  4. FROM tblDistLearnTrack AS tDLT INNER JOIN
  5.      tblPayMonths AS tPM
  6.   ON tDLT.dist_created_date Between tPM.start_date And tPM.end_date
Reply