Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    cmiley is offline Novice
    Windows 10 Access 2013 32bit
    Join Date
    Dec 2016
    Posts
    8

    Find 2 most recent dates


    I am trying to create a query that will return the 2 most recent dates. I currently have my query returning the Max & Min but instead of the Min date I want it to return the next most recent date. Here is what I have so far

    SELECT qryCodeRestrictions.LastName, qryCodeRestrictions.FirstName, qryCodeRestrictions.DateofBirth, qryCodeRestrictions.ProcedureCode, Max(qryCodeRestrictions.DateofService) AS MaxOfDateofService, Min(qryCodeRestrictions.DateofService) AS MinOfDateofService, DateDiff("m",[MinOfDateOfService],[MaxOfDateOfService]) AS DaysBetweenServices, qryCodeRestrictions.Provider
    FROM qryCodeRestrictions
    GROUP BY qryCodeRestrictions.LastName, qryCodeRestrictions.FirstName, qryCodeRestrictions.DateofBirth, qryCodeRestrictions.ProcedureCode, qryCodeRestrictions.Provider

  2. #2
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529
    What about using the Top 2 Values. Look at this video for an idea on how to do this. http://www.datapigtechnologies.com/f...topvalues.html

  3. #3
    cmiley is offline Novice
    Windows 10 Access 2013 32bit
    Join Date
    Dec 2016
    Posts
    8
    Thanks but not quiet what I am looking for. This query is basically finding duplicates of certain procedures of multiple clients and I need the two most recent dates for each client. Using the Top 2 values only gives me the first two clients

  4. #4
    aytee111 is offline Competent At Times
    Windows 7 32bit Access 2013 32bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Query 1: find max date by customer
    Query 2: Query 1 joined to table, max date by customer where date is not equal to query 1 date.

  5. #5
    cmiley is offline Novice
    Windows 10 Access 2013 32bit
    Join Date
    Dec 2016
    Posts
    8
    Thanks for the response. This keeps asking me to enter a parameter value

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    cmiley,

    Better show us the query code that's asking for a parameter.
    Sounds like there an issue with your code.

    Also show sample table records.

  7. #7
    aytee111 is offline Competent At Times
    Windows 7 32bit Access 2013 32bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Parameter usually means that there is a field with square brackets ([ ]) that is unidentified, it isn't in one of the source tables/queries, or on a form that is open.

  8. #8
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722

  9. #9
    cmiley is offline Novice
    Windows 10 Access 2013 32bit
    Join Date
    Dec 2016
    Posts
    8
    Took care of the parameter issue but now I get a syntax error. Here is the SQL code

    SELECT qryCodeRestrictions.LastName, qryCodeRestrictions.FirstName, qryCodeRestrictions.DateofBirth, qryCodeRestrictions.ProcedureCode, qry6MonthCodesMax.LastDateofService, Max(qryCodeRestrictions.DateofService) AS PreviousDateOfService, qryCodeRestrictions.Provider
    FROM qryCodeRestrictions, qry6MonthCodesMax
    GROUP BY qryCodeRestrictions.LastName, qryCodeRestrictions.FirstName, qryCodeRestrictions.DateofBirth, qryCodeRestrictions.ProcedureCode, qry6MonthCodesMax.LastDateofService, qryCodeRestrictions.Provider
    WHERE <>[qry6MonthCodesMax].[LastDateOfService]
    HAVING (((qryCodeRestrictions.ProcedureCode)="D0120" Or (qryCodeRestrictions.ProcedureCode)="D1110" Or (qryCodeRestrictions.ProcedureCode)="D2910" Or (qryCodeRestrictions.ProcedureCode)="D2920" Or (qryCodeRestrictions.ProcedureCode)="D0140"));


    Here is what I want the table to look like
    Click image for larger version. 

Name:	table.jpg 
Views:	28 
Size:	146.4 KB 
ID:	26841

  10. #10
    aytee111 is offline Competent At Times
    Windows 7 32bit Access 2013 32bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    What's this mean: WHERE <>

  11. #11
    cmiley is offline Novice
    Windows 10 Access 2013 32bit
    Join Date
    Dec 2016
    Posts
    8
    not equal to

  12. #12
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Research Select TOP to get the 2 most recent

    http://www.blueclaw-db.com/accessque...select_top.htm

    http://www.w3schools.com/sql/sql_top.asp

    Good luck.

    This just came in while typing
    WHERE <>[qry6MonthCodesMax].[LastDateOfService] ???? you need an expression not equal to what???

  13. #13
    cmiley is offline Novice
    Windows 10 Access 2013 32bit
    Join Date
    Dec 2016
    Posts
    8
    Been there done that. not what I am looking for. That is only giving me the top two records of the whole table and I need the top two records for each customer

  14. #14
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,722
    Review this https://www.accessforums.net/showthr...513#post334513

    Look a the TOP N article here. http://allenbrowne.com/subquery-01.html

    Also we have no idea what your query is doing.

    It may be a better strategy to find the top 2 things you're looking for and then add that as a subquery.

    I don't understand the use MaxDateofService and BirthDate and ProcedureCode.

    Another thing you might consider, if you select the top 1 of something, and then select the Top 1... where the value is not in (select the top 1...) you will get the second top record for the criteria.

    That is, if X is the Top 1 record, then selecting the Top 1 record from the table where the value is not in (X), you will get the second highest/latest record.

  15. #15
    InsuranceGuy is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Aug 2015
    Location
    Colorado
    Posts
    126
    Here's a demo of how to get the last two DOS in Access DBs with the criteria you included in the SQL sample.

    Cheers,


    Jeff

    Last Two Demo DB.mdb

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 12-10-2015, 08:56 AM
  2. Query to find most recent donations
    By KACJR in forum Queries
    Replies: 2
    Last Post: 12-31-2014, 01:57 PM
  3. Need to pull query for 2 Status with recent and prior dates
    By undefatedskillz26 in forum Queries
    Replies: 7
    Last Post: 12-23-2014, 03:33 PM
  4. Replies: 1
    Last Post: 02-21-2014, 05:03 PM
  5. Find most recent measurement through code
    By todmac in forum Programming
    Replies: 6
    Last Post: 08-09-2013, 04:12 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