Results 1 to 6 of 6
  1. #1
    helpaccess is offline Novice
    Windows XP Access 2007
    Join Date
    Feb 2011
    Posts
    18

    TOP subquery


    I am trying to pull the two most recent levels of care scores for each client. However, when I run this below, it returns two records total each from a different client. I would like it to return two records for each client that has two scores. Any ideas? Thank you!

    SELECT LOCUSdates.[CLIENT #], LOCUSdates.[Date Completed], LOCUSdates.[Level Of Care]
    FROM LOCUSdates
    WHERE (((LOCUSdates.[Date Completed]) In (SELECT TOP 2 LOCUSdates.[Date Completed] FROM LOCUSdates WHERE (LOCUSdates.[CLIENT #]) = (LOCUSdates.[CLIENT #]) ORDER BY LOCUSdates.[Date Completed] Desc)) AND ((LOCUSdates.[Level Of Care]) Like "*"));

  2. #2
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    I don't think you'll have luck with this. one record for groups and such is fine, but top 2 or more is a stretch in one query. try writing 2 queries and in the second one, check the first one using a "NOT IN" subquery.

  3. #3
    helpaccess is offline Novice
    Windows XP Access 2007
    Join Date
    Feb 2011
    Posts
    18
    Thanks! Do you know how I would pull just the most recent scores for the first query then?

  4. #4
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Quote Originally Posted by helpaccess View Post
    Thanks! Do you know how I would pull just the most recent scores for the first query then?
    you don't. to be honest, I don't know how to do it. You can do it more than one way.

    try calling a function to do it. With that, you don't need any grouping. All you would need is the TOP 2 clause and a function call with the client identifier in it.

  5. #5
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Check Top N at this site
    http://allenbrowne.com/subquery-01.html

  6. #6
    helpaccess is offline Novice
    Windows XP Access 2007
    Join Date
    Feb 2011
    Posts
    18
    I figured out how to get the most recent date below. Can you elaborate on how to use NOT IN to get the next to last date? Thank you so much!

    SELECT Max([SATS-R LOCUS].[Date Completed]) AS [MaxOfDate Completed], [SATS-R LOCUS].Client
    FROM [SATS-R LOCUS]
    GROUP BY [SATS-R LOCUS].Client
    ORDER BY Max([SATS-R LOCUS].[Date Completed]) DESC;

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

Similar Threads

  1. Subquery and IIF statement
    By ksamaniego in forum Queries
    Replies: 1
    Last Post: 08-15-2011, 03:17 PM
  2. Query with maybe subquery
    By AndycompanyZ in forum Queries
    Replies: 4
    Last Post: 06-28-2011, 11:45 AM
  3. Subquery sum?
    By anemoskkk in forum Access
    Replies: 0
    Last Post: 04-29-2011, 12:36 PM
  4. SQL SubQuery Does Not Work
    By saascuba in forum Access
    Replies: 3
    Last Post: 11-04-2010, 01:59 PM
  5. Subquery
    By combine21 in forum Queries
    Replies: 2
    Last Post: 09-17-2010, 04:33 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