Results 1 to 8 of 8
  1. #1
    Guido is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Nov 2015
    Posts
    2

    Question TOP 5 Per Year...

    I have the following SQL query...



    SELECT TOP 5 SumOfTotal, Year, Customer
    FROM Reporting_MonthlyTotalSales
    GROUP BY Reporting_MonthlyTotalSales.SumOfTotal, Reporting_MonthlyTotalSales.Year, Reporting_MonthlyTotalSales.Customer
    ORDER BY Reporting_MonthlyTotalSales.SumOfTotal DESC

    This returns to me the TOP 5 highest annual SALES totals, by CUSTOMER ie.
    1999 - 200,000 Cust 1
    1999 - 180,000 Cust 25
    2002 - 175,000 Cust 3
    2010 - 130,000 Cust 49
    2004 - 120,000 Cust 1

    But what I actually want is : the TOP 5 sales totals (an thus the Top 5 Customers), for EACH YEAR.

    Help gratefully received, thank you.

    ==================
    Addendum: having posted this elsewhere and having no luck in terms of anyone suggesting an alternative to methods I have tried.... I attach a screenshot of a traditional example I have used that causes Access to get stuck in a loop.

    Click image for larger version. 

Name:	query_pic.png 
Views:	14 
Size:	114.8 KB 
ID:	22637


    I have also tried the following Ranking query suggested on another thread of this site... but clearly I have something wrong... because it doesnt return what is expected....


    SELECT *
    FROM (SELECT a1.SumOfTotal, a1.Customer, a1.Year, COUNT(*) AS CategoryRank
    FROM Reporting_AnnualSalesByCustomer AS a1 INNER JOIN Reporting_AnnualSalesByCustomer AS a2
    ON (a1.CUSTOMER = a2.CUSTOMER) AND (a1.SumOfTotal<= a2.SumOfTotal)
    GROUP BY a1.SumOfTotal, a1.Customer, a1.Year
    ) AS RankingQuery
    WHERE (((RankingQuery.[CategoryRank])<=5) )
    ORDER BY RankingQuery.SumOfTotal, RankingQuery.Customer, RankingQuery.Year;



    Hope someone can help.. having spent most of today on it... its doing my head in....!

    Thank you,

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    Use a 'report table' to store your results. Only do 1 year at a time. append the top 5 of year 2015.
    then append top 5 of 2014., etc.

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Have you tried http://allenbrowne.com/subquery-01.html#TopN

    Year is a reserved word - should not used reserved words as names for anything.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    CJ_London is online now VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,929
    also in your subquery you need to alias your table so the query engine can differentiate it from the one in the main query.

    Not going to write out your whole query but something like

    SELECT *
    FROM myTable
    WHERE myField IN (SELECT TOP 5 myField FROM myTable AS T)

  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,870

  6. #6
    CJ_London is online now VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,929
    @Guido

    thank you for wasting our time - I won't be wasting mine again

  7. #7
    Guido is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Nov 2015
    Posts
    2

    Get a grip

    Quote Originally Posted by Ajax View Post
    @Guido

    thank you for wasting our time - I won't be wasting mine again
    No need to get arsey 'mate'.
    'crossposting' an issue? I wasnt aware either site is affiliated... no big deal... I had figured if more people see a thread on a multitudes of sites... you are more likely to get a response in good time...
    provided either site is updated to advise of a successful outcome...it aint a big deal... well for most people anyway.

    Being in the UK needing to sleep and not monitoring the threads every few minutes, I was about to update this link this morning to advise that it had been answered on the other site... but you got here first...

    In any case, thank you all for your responses.....

  8. #8
    CJ_London is online now VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,929
    read the link provided by Orange

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

Similar Threads

  1. Replies: 1
    Last Post: 10-14-2015, 03:45 PM
  2. Replies: 3
    Last Post: 06-22-2015, 06:36 AM
  3. Replies: 2
    Last Post: 04-25-2014, 11:33 PM
  4. Replies: 4
    Last Post: 01-09-2013, 11:16 AM
  5. Replies: 4
    Last Post: 01-10-2012, 06:26 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