Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6229807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:38:14+00:00 2026-05-24T09:38:14+00:00

I have a report that is based on a query. In the detail section

  • 0

I have a report that is based on a query. In the detail section of the report I want to limit the number of detail lines to top N. (like a select TOP N type query). In this sample I want the top 2 rows. Query I have so far is

 SELECT TestTable.[GroupByCol], TestTable.[DetailColA]
 FROM TestTable group by TestTable.[GroupByCol], TestTable.[DetailColA]

I know I need to join and do some having and count but it’s just not clicking. I have also attached a screenshot.

Please advise. thank you
enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:38:16+00:00Added an answer on May 24, 2026 at 9:38 am

    Try the following query as your report recordsource:

    SELECT  TestTable.GroupByCol, TestTable.DetailColA
    FROM TestTable 
    WHERE TestTable.DetailColA IN 
      (SELECT TOP 2 DetailColA FROM TestTable AS TT 
       WHERE TT.GroupByCol=TestTable.GroupByCol 
       ORDER BY TT.DetailColA) 
    ORDER BY TestTable.GroupByCol, TestTable.DetailColA
    

    See Allen Browne’s Subquery Basics for more information.


    EDIT: As you alluded to in your comment, this can also be written as follows:

    SELECT a.GroupByCol, a.DetailColA, COUNT(*) As RankNumber
    FROM TestTable AS a INNER JOIN TestTable AS b
      ON a.GroupByCol = b.GroupByCol
     AND a.DetailColA >= b.DetailColA
    GROUP BY a.GroupByCol, a.DetailColA
    HAVING COUNT(*)<=2
    ORDER BY a.GroupByCol, a.DetailColA, COUNT(*) 
    

    You would have to benchmark to be sure, but this second version is probably more efficient. Which is more readable is probably a matter of personal preference (I find the first version more intuitive and readable).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a report in SSRS 2005 that's based on a query that's similar
I have a MS Access 2007 report which is based on a query that
So I have a query that is a Top Nth aggregate query, and I
scenario: I have difficulty in building a single query that would display a report
Ok, so I want to have a report that does not require the end
I have a report in Access that we want split and exported to .rtf
I have a third-party app that creates HTML-based reports that I need to display.
I have a report that uses a TChart that I am maintaining. One of
I have a report that renders data returned from a stored procedure. Using profiler
I have a report that is used by a windows service and a form

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.