Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2008-06-18
6
Medium Priority
?
1,272 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
  • 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 52

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 52

Accepted Solution

by:
Gustav Brock earned 375 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Ready to get certified? Check out some courses that help you prepare for third-party exams.
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

963 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