Solved

Adding Rank to a table

Posted on 2013-01-08
13
282 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 38757721
What about Ties?

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

Expert Comment

by:Rey Obrero (Capricorn1)
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 120

Accepted Solution

by:
Rey Obrero (Capricorn1) 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 120

Assisted Solution

by:Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1) 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

820 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