Data Definition queries

Tlewman

Board Regular
Joined
Dec 29, 2002
Messages
67
I am trying to alter a table with a data definition query automatically adds a column that ranks another column in the table (say sales) from highest to lowest. I am able to get the query to add the column named rank, but am having trouble getting it to automatically populate the column with the rankings. This info will feed a report that I will need to update every month. Any thoughts ???
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Here is an example that should help. Below is a sample of my table:

<table BORDER="1" BGCOLOR="#ffffff" CELLSPACING="0"><tbody><tr><td>Customer</td><td>Contract</td></tr><tr VALIGN="TOP"><td>A</td><td>c13514</td></tr><tr VALIGN="TOP"><td>A</td><td>c15097</td></tr><tr VALIGN="TOP"><td>A</td><td>c21201</td></tr><tr VALIGN="TOP"><td>A</td><td>c27169</td></tr><tr VALIGN="TOP"><td>A</td><td>c31652</td></tr><tr VALIGN="TOP"><td>A</td><td>c34277</td></tr><tr VALIGN="TOP"><td>A</td><td>c46318</td></tr><tr VALIGN="TOP"><td>A</td><td>c46421</td></tr><tr VALIGN="TOP"><td>A</td><td>c46517</td></tr><tr VALIGN="TOP"><td>B</td><td>c12399</td></tr><tr VALIGN="TOP"><td>B</td><td>c1495</td></tr></tbody> </table>

My table name is tblCustomers. Create a new query, add the 2 fields, and sort ascending by the first field and descending by the second (your "sales" field - this is because you want to rank from highest to lowest). Then right-click on the table in your query and select Properties. Under Alias, put X (or something else to your liking). Then in a new column in your query put something like this:

Rank: (SELECT Count(*) FROM tblCustomers WHERE tblCustomers.Customer = X.Customer AND tblCustomers.Contract > X.Contract) + 1

Here is my entire query:

SELECT X.Customer, X.Contract, (SELECT Count(*) FROM tblCustomers WHERE tblCustomers.Customer = X.Customer AND tblCustomers.Contract > X.Contract)+1 AS Rank
FROM tblCustomers AS X
ORDER BY X.Customer, X.Contract DESC;

And here are my query results:

<table BORDER="1" BGCOLOR="#ffffff" CELLSPACING="0"><tbody><tr VALIGN="TOP"><td>Customer</td><td>Contract</td><td>Rank</td></tr><tr VALIGN="TOP"><td>A</td><td>c46517</td><td ALIGN="RIGHT">1</td></tr><tr VALIGN="TOP"><td>A</td><td>c46421</td><td ALIGN="RIGHT">2</td></tr><tr VALIGN="TOP"><td>A</td><td>c46318</td><td ALIGN="RIGHT">3</td></tr><tr VALIGN="TOP"><td>A</td><td>c34277</td><td ALIGN="RIGHT">4</td></tr><tr VALIGN="TOP"><td>A</td><td>c31652</td><td ALIGN="RIGHT">5</td></tr><tr VALIGN="TOP"><td>A</td><td>c27169</td><td ALIGN="RIGHT">6</td></tr><tr VALIGN="TOP"><td>A</td><td>c21201</td><td ALIGN="RIGHT">7</td></tr><tr VALIGN="TOP"><td>A</td><td>c15097</td><td ALIGN="RIGHT">8</td></tr><tr VALIGN="TOP"><td>A</td><td>c13514</td><td ALIGN="RIGHT">9</td></tr><tr VALIGN="TOP"><td>B</td><td>c50947</td><td ALIGN="RIGHT">1</td></tr><tr VALIGN="TOP"><td>B</td><td>c2485</td><td ALIGN="RIGHT">2</td></tr><tr VALIGN="TOP"><td>B</td><td>c21751</td><td ALIGN="RIGHT">3</td></tr><tr VALIGN="TOP"><td>B</td><td>c1495</td><td ALIGN="RIGHT">4</td></tr><tr VALIGN="TOP"><td>B</td><td>c12399</td><td ALIGN="RIGHT">5</td></tr></tbody> </table>

HTH,

Russell
 
Upvote 0
Perfect....
Now another question. I have a couple of instances where I may have five of the same customer but only three of those five have sales for the week. The other two customers have blank cells under the sales column so when the rank is added, it ranks each of them as 1 instead of 4 or 5.

So my info looks like

cust1 500 1
cust1 400 2
cust1 300 3
cust1 1
cust1 1

How do I fix this?
 
Upvote 0
Do you really want them included in the ranking? You could exclude them by typing "is not null" in the criteria line for SALES.

If you really want them in your ranking, let me know and I'll see what I can do.

-rh
 
Upvote 0
I have to have them included because I have another column in this report that shows year to date sales. It is ranked by the current weeks sales but I have to show YTD info as well.
 
Upvote 0

Forum statistics

Threads
1,221,517
Messages
6,160,264
Members
451,635
Latest member
nithchun

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top