Results 1 to 3 of 3
  1. #1
    Cottonshirt is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Feb 2018
    Posts
    116

    query the max(date) that is not > date in current record

    I have a select query that has in it a date field: qry_record.date



    the dates are listed in ascending order, latest at the bottom.

    I also have a table, that has in it a date field: tbl_race.date

    I want to put the two together in a further query, so that, for each record in tbl_race, I have the latest date from qry_record that is not greater than (i.e later than) tbl_race.date

    so, if the dates in qry_record look like this:

    12-Feb-1956
    14-Jun-1964
    17-Sep-1965

    then the output of my qry should look something like this:
    race.date record.date
    11-Jun-1964 12-Feb-1956
    13-Jun-1964 12-Feb-1956
    14-Jun-1964 14-Jun-1964
    17-Jun-1964 14-Jun-1964

    what I've tried:

    I ran this query:
    Code:
    SELECT tbl_race.date, Max(qry_record.date) AS MaxOfdate
    FROM tbl_race LEFT JOIN qry_record ON tbl_race.date = qry_record.date
    GROUP BY tbl_race.date
    ORDER BY tbl_race.date;
    and that gave me the following:

    race.date record.date
    29-Nov-1962
    25-May-1963 25-May-1963
    11-Jun-1963
    12-Sep-1964 12-Sep-1964

    which feels like progress because where the record changes is marked, but the intermediate dates are left blank and I would need some way of filling in the gaps.

    gratefull for any help, thank you.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,815
    This involves a Cartesian query. Remove the JOIN clause. Consider:

    SELECT tbl_race.Date, Max(qry_record.date) AS MaxOfdate
    FROM tbl_race, qry_record
    WHERE (((qry_record.date)<=[tbl_race].[Date]))
    GROUP BY tbl_race.Date
    ORDER BY tbl_race.Date;

    Date is a reserved word. Really should not use reserved words as names for anything.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Cottonshirt is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Feb 2018
    Posts
    116
    I hadn't actually realised that it was possible to have two sources in a query, and NOT have them joined. so your solution came as a real surprise to me.

    it also worked. thank you very much indeed.

    Date is a reserved word. Really should not use reserved words as names for anything.
    I don't. none of the names in my question are real.

    I changed them to make the question more easily comprehensible.

    thank you.

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

Similar Threads

  1. Replies: 1
    Last Post: 10-12-2017, 07:19 PM
  2. Replies: 3
    Last Post: 05-10-2016, 11:51 AM
  3. Replies: 3
    Last Post: 03-02-2016, 09:39 AM
  4. Replies: 6
    Last Post: 12-11-2012, 09:40 PM
  5. Replies: 25
    Last Post: 11-16-2012, 12:47 PM

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