Solved

Adding Rank to a table

Posted on 2013-01-08
13
280 Views
Last Modified: 2013-01-09
Hey everyone,

Can someone look at the attached DB and tell me how to add a rank to an additional field?

I started with a query and had my data sorted the way I needed it.  I coudn't figure out how to add a rank to the query.  The table is my data as I queried it.

Thanks for any suggestion.

MC
Ranking.accdb
0
Comment
Question by:MCaliebe
  • 6
  • 3
  • 3
  • +1
13 Comments
 
LVL 26

Expert Comment

by:jerryb30
ID: 38757631
What do you mean by rank? Number sold? Number sold by Item?
0
 

Author Comment

by:MCaliebe
ID: 38757660
Rank the item number by the Total Sales
Higest sales value is 1, next value is 2, ect.
0
 

Author Comment

by:MCaliebe
ID: 38757663
In Excel, I would normally sort by the sales volume and add a column for Rank, and copy down 1~ however many records exist.  This isn't as easy in Access.  

I have to pull a number of queries on sales by product code and rank the products by total sales volume so this is going to have to be repeated.
0
 
LVL 26

Expert Comment

by:jerryb30
ID: 38757667
So pr_cd will always be the same or a given item_number?
0
 
LVL 26

Expert Comment

by:jerryb30
ID: 38757675
And the combination of pr_cd is unique?
SELECT a.PR_CD, a.ITEM_NUMBER, a.Qty_Sold_Total, DCount("*","rank","qty_sold_total > " & [a].[qty_sold_total])+1 AS seq
FROM Rank AS a;
0
 

Author Comment

by:MCaliebe
ID: 38757708
Actually, I have multiple product codes PR_CD in my large table.  I have about 15000 recodes with 17 product codes.  

For each product code, I need to rank the Item_Numbers (Unique) by sales dollars.  I attached the DB again with a larger sampling of data.

Once I go through this exercise, I need to rank the items again by Qty_Sold, however I assume it will be using the same techniques I learn from this inquiry.

Thanks for your help.
Ranking.accdb
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 38757721
What about Ties?

The highest value is 200, but two records are 200?
0
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 38757731
see how to do this from this link

How to Rank Records Within a Query
http://support.microsoft.com/kb/208946
0
 

Author Comment

by:MCaliebe
ID: 38757759
I tried to use that method for adding a rank and what I believe was happening was when I queried the data, I got skips in my ranks ie. 1,2,7,21,35,37 ect. I believe I was ranking my entire source table, However as I placed criteria for the pr_cd, "STF", the balance was filtered out producing the inconsistent ranking.

As related to ties, I can leave them as duplicates and sort by item number...simply because I wouldn't have any further data to differentiate the ties.
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 250 total points
ID: 38757778
try this query

SELECT tbl_sample_data.PR_CD, tbl_sample_data.ITEM_NUMBER, tbl_sample_data.Sales_Total, (select count(*) from tbl_sample_data T where [pr_cd]=tbl_sample_data.pr_cd and [Sales_Total]>=tbl_sample_data.[Sales_Total]) AS Ranking
FROM tbl_sample_data
WHERE (((tbl_sample_data.PR_CD)="STF"))
ORDER BY tbl_sample_data.Sales_Total DESC;

or this one


SELECT tbl_sample_data.PR_CD, tbl_sample_data.ITEM_NUMBER, tbl_sample_data.Qty_Sold_Total, (select count(*) from tbl_sample_data T where [pr_cd]=tbl_sample_data.pr_cd and [Qty_Sold_Total]>=tbl_sample_data.[Qty_Sold_Total]) AS Ranking
FROM tbl_sample_data
WHERE (((tbl_sample_data.PR_CD)="STF"))
ORDER BY tbl_sample_data.Qty_Sold_Total DESC;
0
 

Author Comment

by:MCaliebe
ID: 38758736
Cap,

Your suggestions work excellent.  Can I ask you do one more thing for me...a quick explaination of what your query is doing?

I believe I understand everything but this line

(select count(*) from tbl_sample_data T where [pr_cd]=tbl_sample_data.pr_cd and [Sales_Total]>=tbl_sample_data.[Sales_Total]) AS Ranking

What is T?
...and [Sales_Total]>=... does this refer to the field in the current query?
0
 
LVL 119

Assisted Solution

by:Rey Obrero
Rey Obrero earned 250 total points
ID: 38758826
T is just an alias of the table created in memory as a copy of tbl_sample_data.. you can write the line this way too, sorry i did the shortcut

(select count(*) from tbl_sample_data T where T.[pr_cd]=tbl_sample_data.pr_cd and T.[Sales_Total]>=tbl_sample_data.[Sales_Total]) AS Ranking
0
 

Author Closing Comment

by:MCaliebe
ID: 38758857
Thanks for taking the time to teach...  Works perfect.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now