Results 1 to 3 of 3
  1. #1
    kaylachris is offline Novice
    Windows Vista Access 2007
    Join Date
    May 2010
    Posts
    21

    Nested Query, Return most recent record


    Attempting to nest a query inside another query but uncertain of the correct syntax. The following table

    Table: lclTempImportHW
    LastName FirstName MiddleName DateRecorded BodyFatPass BodyFatPercent Height Weight HeightWeightPass


    This query successfully returns the most recent date for each individual.
    Code:
    SELECT Max(lclTempImportHW.DateRecorded) AS MaxOfDateRecorded, lclTempImportHW.LastName, lclTempImportHW.FirstName, lclTempImportHW.MiddleName
    FROM lclTempImportHW
    GROUP BY lclTempImportHW.LastName, lclTempImportHW.FirstName, lclTempImportHW.MiddleName;
    However, I need the rest of the columns of data to go along with these dates as well as pulling the StudentID from tbl_StudentData...

    Code:
    SELECT tbl_StudentData.StudentID_PK, lclTempImportHW.*
    FROM tbl_StudentData INNER JOIN ((lclTempImportHW ON (tbl_StudentData.F_Name = lclTempImportHW.FirstName) AND (tbl_StudentData.M_Name = lclTempImportHW.MiddleName) AND (tbl_StudentData.L_Name = lclTempImportHW.LastName)) INNER JOIN (SELECT Max(lclTempImportHW.DateRecorded) AS MaxOfDateRecorded, lclTempImportHW.LastName, lclTempImportHW.FirstName, lclTempImportHW.MiddleName
    FROM lclTempImportHW
    GROUP BY lclTempImportHW.LastName, lclTempImportHW.FirstName, lclTempImportHW.MiddleName) as maxDateQry ON (maxDateQry.MaxOfDateRecorded) AND (lclTempImportHW.LastName = maxDateQry.LastName) AND (lclTempImportHW.FirstName = maxDateQry.FirstName) AND (lclTempImportHW.MiddleName = maxDateQry.MiddleName) ;
    This is what I have so far but gives me a "Syntax error in JOIN operation"

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    A two query solution:

    http://www.baldyweb.com/LastValue.htm

    It can be done as a subquery, but is harder to maintain that way in my opinion.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    kaylachris is offline Novice
    Windows Vista Access 2007
    Join Date
    May 2010
    Posts
    21
    That's what I had originally done. However, I have a total of 6 tables that are populated from .transferspreadsheet. Each contains different sets of data but all contain the same fields for names and DateRecorded. I need the most recent data for each person from each table. My intent is to hard code the SQL statement and inject the different table names with variables so I can pull it into a recordset and manipulate the data (i.e. trim extra spaces and normalize). After all that move it to its appropriate permanent table.

    I'm sure this can all be done with one massive query but I wouldn't even know where to begin to start writing that. Furthermore, using dao I can through up a progress bar so users dont think the program crashed as it pushes through all this data.

    I also considered creating a excel object.. merging all 6 tables then importing as one table. But from what I've read this would be the most cpu intensive process of them all.

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

Similar Threads

  1. Replies: 5
    Last Post: 09-22-2015, 12:18 PM
  2. Query for most recent record
    By Icon in forum Queries
    Replies: 10
    Last Post: 01-15-2014, 06:27 AM
  3. Replies: 1
    Last Post: 08-25-2011, 09:50 AM
  4. Replies: 4
    Last Post: 05-04-2010, 03:33 PM
  5. Return most recent entry for each item
    By GenAp in forum Queries
    Replies: 1
    Last Post: 02-04-2010, 05:30 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