Hi there,
I am trying to create an SQL query to find the record that corresponds to the earliest date in one table, that is after a certain date in another table.
I have two tables, tblMain and tblApplicants.
In tblDates, there are 2 columns with 20 records. In tblApplicants, there are 3 columns, but over 100 records.
tblMain looks like this:
DateID, Date
1 01/01/2010
2 10/01/2010
3 25/01/2010
4 02/02/2010
etc
tblApplicants looks like this:
FirstName, LastName, DateofApplication
Joe Blogs 16/01/2010
John Smith 04/02/2010
etc
Basically, for every record in tblApplicants, I am trying to find the earliest date (and corresponding DateID) in tblMain, where the Date is later than the DateofApplication in tblApplicants.
For example, if the DateofApplication is 16/01/2010, the earliest date after this in tblMain would be 25/01/2010 and the corresponding DateID would be 3.
I have attempted to create an SQL query for this as shown below, but I’m not entirely sure how to construct it and, moreover, how to get it work for every single record in tblApplicants.
SELECT tblMain.DateID, tblMain.[Date]
FROM tblApplicants
WHERE tblMain.[Date] = ((SELECT MIN(tblMain.[Date])
FROM tblMain
WHERE tblMain.[Date] > (SELECT DateofApplication FROM tblApplicants))
Any help would be greatly appreciated.
Thanks,
Marco
I am trying to create an SQL query to find the record that corresponds to the earliest date in one table, that is after a certain date in another table.
I have two tables, tblMain and tblApplicants.
In tblDates, there are 2 columns with 20 records. In tblApplicants, there are 3 columns, but over 100 records.
tblMain looks like this:
DateID, Date
1 01/01/2010
2 10/01/2010
3 25/01/2010
4 02/02/2010
etc
tblApplicants looks like this:
FirstName, LastName, DateofApplication
Joe Blogs 16/01/2010
John Smith 04/02/2010
etc
Basically, for every record in tblApplicants, I am trying to find the earliest date (and corresponding DateID) in tblMain, where the Date is later than the DateofApplication in tblApplicants.
For example, if the DateofApplication is 16/01/2010, the earliest date after this in tblMain would be 25/01/2010 and the corresponding DateID would be 3.
I have attempted to create an SQL query for this as shown below, but I’m not entirely sure how to construct it and, moreover, how to get it work for every single record in tblApplicants.
SELECT tblMain.DateID, tblMain.[Date]
FROM tblApplicants
WHERE tblMain.[Date] = ((SELECT MIN(tblMain.[Date])
FROM tblMain
WHERE tblMain.[Date] > (SELECT DateofApplication FROM tblApplicants))
Any help would be greatly appreciated.
Thanks,
Marco