Results 1 to 6 of 6
  1. #1
    Mattbro is offline Competent Performer
    Windows XP Access 2010 64bit
    Join Date
    Mar 2012
    Posts
    100

    Query to find nearest coming date on Table2 (End of range) from date on Table 1 (event date)

    Hi all. I have a db with two tables:



    TbTest which has [TestNumber], [TestDate], [TestMethod] and PK-a concatenated filed of [TestNumber] and [Test Method] with serves as the PK.

    TbDateRange which gives the [TestMethod], the date range of the parts used-[DateStart] and [DateEnd], and the relevant serial number of the test equipment [BatchNo].

    I am trying to write a query that will give me the batch number of the part used for each test in TbTest, e.g, [TestNumber],[BatchNo], based upon the [TestDate] and [DateEnd].

    Code:
    SELECT TbTest.TestNumber, TbTest.TestMethod, TbTest.TestDate, TbDateRange.DateEnd, DateDiff("d",[DateEnd],[TestDate]) AS Expr1FROM TbDateRange, TbTest
    WHERE (((TbTest.TestMethod)="ELECTRODE") AND ((DateDiff("d",[DateEnd],[TestDate]))>0))
    ORDER BY DateDiff("d",[DateEnd],[TestDate]);
    Im failing miserably. If anyone has any advice, it would be appreciated.



    Many thanks,
    Mattbro451
    Attached Files Attached Files

  2. #2
    madpiet is offline Expert
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    566
    Something like this?
    You don't *have* to join on an equality... you can use >=,>,<,<=

    SELECT TbTest.TestNumber, TbTest.ID, MIN(TbTest.TestDate)
    FROM TbTest INNER JOIN TbDateRange ON TbTest.TestMethod = TbDateRange.Method
    WHERE (((TbTest.TestDate)>=[DateStart] And (TbTest.TestDate)<=[DateEnd]))
    GROUP BY tbTest.TestNumber, tbTest.ID;

  3. #3
    madpiet is offline Expert
    Windows 10 Office 365
    Join Date
    Feb 2023
    Posts
    566
    or by "nearest" did you mean MIN(ABS(DATEDIFF(...))?

  4. #4
    Mattbro is offline Competent Performer
    Windows XP Access 2010 64bit
    Join Date
    Mar 2012
    Posts
    100
    Hi Madpiet-thanks for the input. I've tried your suggestion, but I can't see how to apply it to get the desired results. Your query gives the following;
    Click image for larger version. 

Name:	I1.jpg 
Views:	20 
Size:	34.5 KB 
ID:	51845

    I need to extend it to give the range from TbDateRange and thus the appropriate batchno. What I am trying to produce is;
    Click image for larger version. 

Name:	I2.jpg 
Views:	20 
Size:	50.4 KB 
ID:	51846
    If the testdate can be used to show the closest approaching DateEnd, I should be able to find the appropriate batch no used.
    Any ideas? K/R, Mattbro

  5. #5
    xps35's Avatar
    xps35 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jun 2022
    Location
    Schiedam, NL
    Posts
    299
    Try
    Code:
    SELECT TbTest.TestNumber, TbTest.ID, TbDateRange.DateStart, TbDateRange.DateEnd, TbDateRange.BatchNoFROM TbDateRange, TbTest
    WHERE TbTest.TestMethod=TbDateRange.Method AND 
          TbTest.TestDate>=DateStart And 
          TbTest.TestDate<=DateEnd
    Groeten,

    Peter

  6. #6
    Mattbro is offline Competent Performer
    Windows XP Access 2010 64bit
    Join Date
    Mar 2012
    Posts
    100
    That is fantastic. Many thanks Peter!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 11-20-2018, 11:57 PM
  2. Find the Nearest Date from another table
    By wizzz_wizzz in forum Queries
    Replies: 2
    Last Post: 03-30-2017, 12:14 PM
  3. Replies: 3
    Last Post: 03-17-2017, 08:52 AM
  4. Replies: 10
    Last Post: 12-15-2016, 05:52 PM
  5. Replies: 10
    Last Post: 05-15-2015, 11:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums