Solved

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

Posted on 2008-06-18
6
1,198 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 50

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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 

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 50

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

710 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