Solved

Access Ranking Query results in 1st place tie showing as 2nd place ties

Posted on 2008-06-18
6
1,224 Views
Last Modified: 2013-11-05
I have used the following code from MSDN to create a ranking query.  But when I change the data to show 2 first place ties, the query assigns rank position 2 to both ties, not 1 to both:

Product Name      TotalSales      Rank
Camembert Pierrot      1496      2
Raclette Courdavault      1496      2
Gorgonzola Telino      1397      3
Gnocchi di nonna Alice      1263      4
Pavlova      1158      5
Rhönbräu Klosterbier      1155      6

I need both 1st place ties to show as rank 1.

CODE:
SELECT ProductName,
  SUM(Quantity) AS TotalSales
FROM [Order Details] INNER JOIN Products
ON [Order Details].ProductID =
               Products.ProductID
GROUP BY ProductName
ORDER BY SUM(Quantity) DESC

After that query has been saved as qryProductTotals, you can use this query with a subquery to generate the sales rankings:

SELECT ProductName, TotalSales,
  (SELECT COUNT(*)
   FROM qryProductTotals AS QPT
   WHERE qryProductTotals.TotalSales
           <=  QPT.TotalSales) AS Rank
FROM qryProductTotals
ORDER BY TotalSales DESC
from page: http://msdn.microsoft.com/en-us/library/aa217680(office.11).aspx

0
Comment
Question by:mleeman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 21813303
Is there an autoincrement column on your table?
0
 

Author Comment

by:mleeman
ID: 21813422
The data is from the sample database downloed from microsoft.  

Products table does have ProductID field set to Autonumber.  The data is from the MS Access sample northwind database
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 21813533
How about:

SELECT ProductName, TotalSales,
  (SELECT COUNT(*)
   FROM qryProductTotals AS QPT
   WHERE qryProductTotals.TotalSales
           <  QPT.TotalSales) + 1 AS Rank
FROM qryProductTotals
ORDER BY TotalSales DESC

/gustav
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

Author Comment

by:mleeman
ID: 21813759
When I did that, it did fix the 2 first places, but then it bumped 2nd place to 3rd.   With this query if there is not a tie the 1-3 works fine, but if 1st or 2nd is tied, the rank following a tie is also bumped up +1.

Product Name      TotalSales      Rank
Camembert Pierrot      1496      1
Raclette Courdavault      1496      1
Gorgonzola Telino      1397      3
Gnocchi di nonna Alice      1263      4
Pavlova      1158      5
0
 
LVL 51

Accepted Solution

by:
Gustav Brock earned 125 total points
ID: 21813901
Then you could use an in-between query to create a distinct list of TotalSales:

SELECT
  ProductName,
  SUM(Quantity) AS TotalSales
FROM
  [Order Details]
INNER JOIN
  Products
    ON
    [Order Details].ProductID = Products.ProductID
GROUP BY
  ProductName

SELECT DISTINCT
  TotalSales
FROM
  qryProductTotals

Save this as qryProductTotalsDistinct.
Then:

SELECT
  ProductName,
  TotalSales,
  (SELECT COUNT(*)
   FROM qryProductTotalsDistinct AS QPT
   WHERE qryProductTotals.TotalSales
           <=  QPT.TotalSales) AS Rank
FROM
  qryProductTotals
ORDER BY
  TotalSales DESC

/gustav
0
 

Author Comment

by:mleeman
ID: 21817204
When  did that everything ranked as 70.

I did more research on MS KB 208946 and got it to work.

Qry1:
SELECT Products.ProductName, Sum([Order Details].Quantity) AS TotalSales
FROM [Order Details] INNER JOIN Products ON [Order Details].ProductID = Products.ProductID
GROUP BY Products.ProductName
ORDER BY Sum([Order Details].Quantity) DESC;


Qry2:
SELECT DISTINCT Count(qry1.ProductName) AS CountOfProductName, qry1.TotalSales
FROM qry1
GROUP BY qry1.TotalSales
ORDER BY qry1.TotalSales DESC;


Qry3:
SELECT [qry1-alias].ProductName, [qry1-alias].TotalSales, (Select Count(*) from [qry2] Where  
   [TotalSales] > [qry1-Alias].[TotalSales])+1 AS Ranking2
FROM qry1 AS [qry1-alias]
ORDER BY [qry1-alias].TotalSales DESC;

Result:
Product Name      TotalSales      Ranking2
Camembert Pierrot      1496      1
Raclette Courdavault      1496      1
Gorgonzola Telino      1496      1
Gnocchi di nonna Alice      1263      2
Pavlova      1158      3
Rhönbräu Klosterbier      1155      4
Guaraná Fantástica      1125      5
Boston Crab Meat      1103      6


Thank you for the help, you got me on the right track.

0

Featured Post

ClickHouse in a General Analytical Workload

We have mentioned ClickHouse in some recent posts, where it showed excellent results.

In this article on Experts Exchange, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question