Results 1 to 2 of 2
  1. #1
    MsAxes is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2021
    Posts
    100

    How to return top N records for each group?

    Hi all - I have a table (DataTab) see ex. below.



    _ID ssn
    cell 123456789
    cell 123456789
    cell 123456789
    cell 321000000
    cell 321000000
    cell 654000000
    screen_2 654000000
    screen_2 654000000
    screen_2 654000000
    screen_2 987000000
    screen_2 987000000
    screen_2 987000000
    screen_2 987654321
    screen_2 987654321


    I would like to return the top 2 count of SSNs by ID (see expected results below).
    ID ssn Count
    cell 123456789 3
    cell 321000000 2
    screen_2 654000000 3
    screen_2 987000000 3


    I have the SQL below but when I run it I am asked to enter the paramter values for T2.CountID and I can't figure out why.

    Code:
    SELECT T1.*
    FROM DataTab as T1
    WHERE T1.[_ID] IN (
                                            SELECT TOP 2 Count(T2.[_ID]) as CountID
                                            FROM DataTab as T2
                                            WHERE T2.SSN = T1.SSN
                                            ORDER BY T2.CountID DESC)
    ORDER BY SSN, [_ID];

  2. #2
    Gicu's Avatar
    Gicu is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,115
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

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

Similar Threads

  1. Replies: 2
    Last Post: 11-26-2016, 01:07 PM
  2. Replies: 9
    Last Post: 08-19-2014, 12:41 PM
  3. Option Group To Return Text Instead of a Value
    By justair07 in forum Access
    Replies: 3
    Last Post: 08-21-2013, 02:31 PM
  4. Replies: 1
    Last Post: 02-13-2012, 04:58 PM
  5. Newbie trying to Return Top 5 for Each in a Group
    By deluga.69 in forum Programming
    Replies: 1
    Last Post: 08-09-2011, 08:27 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