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 8109843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:32:46+00:00 2026-06-06T01:32:46+00:00

I would like to use the top 5 to capture the top 5 for

  • 0

I would like to use the top 5 to capture the top 5 for a number of criteria.

The data style is month date, area1, subarea1, sub areas2 etc etc, type, unique identifier for each row.

What I’m trying to do is for each month and for area find the top 5 types.

Sample Data:


Date              Division        Sub Division       ID 
31/05/2012        Sales                              001
31/05/2012        Sales           Call Centre        002
31/05/2012        Sales           Call Centre        003
31/05/2012        Sales           Store              004
31/05/2012        Marketing                          005
31/05/2012        Marketing        TV                006
30/04/2012        Sales                              001
30/04/2012        Sales           Call Centre        002
30/04/2012        Sales           Call Centre        003
30/04/2012        Sales           Store              004
30/04/2012        Marketing                          005
30/04/2012        Marketing        TV                006

And so on.

The output I’m after is:
Top 5 at Division level (no consideration of Sub Division) for each month
Top 5 at each Sub-Division level for each month

Sample Output:

Date          Top5Areas     Volume
31/05/2012    Sales         100
31/05/2012    Marketing     90
31/05/2012    HR            50
30/04/2012    Sales         100
30/04/2012    Marketing     90
30/04/2012    HR            50

And depending on the level the sub division for the respective area:

Date          Top5Areas     Volume
31/05/2012    Call Centre   100
31/05/2012    Store         90
31/05/2012    HR            50 

but with 5 for each group.

I'm not should how to do this and wonder if I'll need to build up the result through a number of queries or whether there is a better way.

Thanks in advance and appreciate your suggestions.

    SELECT [TableA].[DisplayVar] AS DisplayVar, [TableB].[Date] AS MonthDate, TableC.        [Divison] AS Divison, Volume
    FROM (SELECT [TableA].[DisplayVar],
    [TableB].[Date],
    TableC.[Divison],
    Volume
      (  SELECT  COUNT() + 1
      FROM    (   SELECT [TableA].[DisplayVar], [TableB].[Date], TableC.[Divison], COUNT()     AS Volume
         FROM    ([TableA] INNER JOIN [TableB] ON [TableA].[ID] = [TableB].[ID]) LEFT JOIN     TableC ON [TableB].ID = TableC.Descriptor
         GROUP BY [TableA].[DisplayVar], [TableB].[Date], TableC.[Divison]
      ) AS T
      WHERE   T.[TableB].[Date] = Data.[TableB].[Date]
      AND     T.Volume > Data.Volume
    ) AS Rank
    FROM    (   SELECT  [TableA].[DisplayVar], [TableB].[Date], TableC.[Divison], COUNT(*)     AS Volume
      FROM    ([TableA] INNER JOIN [TableB] ON [TableA].[ID] = [TableB].[ID]) LEFT JOIN     TableC ON [TableB].ID = TableC.Descriptor
      WHERE   [TableB].[Date] BETWEEN Date() AND DateADD("m", -12, Date())
GROUP BY[TableA].[DisplayVar], [TableB].[Date], TableC.[Divison] ) AS DATA ) AS Data WHERE (((Data.Rank)<=5)) ORDER BY [TableB].[Date] DESC , Volume DESC;

  • 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-06-06T01:32:48+00:00Added an answer on June 6, 2026 at 1:32 am

    The only way I can think of to do a top n per date (Which is what I think you are after) in access is to use a correlated subquery. I think the simplest method is similar to using the ROW_NUMBER() function in other DBMS, it adds an additional field to your orignal aggregate query giving each division/date a rank, the the outer most where clause limits it to rows where the rank is less than or equal to 5.

    SELECT  Date,
            Division,
            Volume
    FROM    (   SELECT  Date,
                        Division,
                        Volume,
                        (   SELECT  COUNT(*) + 1
                            FROM    (   SELECT  Date, Division, COUNT(*) AS Volume
                                        FROM    YourTable
                                        GROUP BY Date, Division
                                    ) AS T
                            WHERE   T.Date = Data.Date
                            AND     T.Volume > Data.Volume
                        ) AS Rank
                FROM    (   SELECT  Date, Division, COUNT(*) AS Volume
                            FROM    YourTable
                            WHERE   Date BETWEEN DATE() AND DATEADD("m", -12, DATE())
                            GROUP BY Date, Division
                        ) AS DATA
            ) AS Data
    WHERE   Rank <= 5
    

    The downfall (or benefit depending on your requirements) of this is that it does not deal with ties, i.e if you had 10 divisions with the same volume all 10 would be returned, not just 5 of them.

    I am not sure if this is exactly what you require, but hopefully will is at least get you going in the right direction.

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

Sidebar

Related Questions

I would like to use Tree Apache Wicket to implement the following opeation: Top
I have an array of Integers in Java, I would like use only a
I would like to use R to extract the speaker out of scripts formatted
I would like to use Maven's password encryption such as it uses for nodes
I would like to use the logout function from Django but not sure how
I would like to use D3.js (or maybe Raphaël ) for backend-generated reports using
I would like to use the jenkins script console some more. Where do I
I would like to use a pre-commit hook in riak to validate the json
I would like to use GIO (which is part of GLIB) on Android by
I would like to use the MFMailComposeViewController mailComposeDelegate property with completion block syntax, but

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.