473,569 Members | 2,793 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Rank records automatically

max
Let's say I have a table with the following fields. State, Customer,
and Total Sales. What I want to do is rank each customer by sales
volume by state. i.e. highest sales is ranked 1, second highest ranked
2, etc within each state.

I think I could do this in a cumbersome fashion by using the max
function, appending, deleting and using the max function again for #2
and so on. I was wondering if there is a built in function or a piece
of code or expression that would assign these rankings automatically in
a new field.

Which brings me to this. I guess I need to learn SQL. Are there any
web resources or books that anyone can recommend? Thanks

max

Oct 7 '06 #1
3 6553
max wrote:
Let's say I have a table with the following fields. State, Customer,
and Total Sales. What I want to do is rank each customer by sales
volume by state. i.e. highest sales is ranked 1, second highest ranked
2, etc within each state.

I think I could do this in a cumbersome fashion by using the max
function, appending, deleting and using the max function again for #2
and so on. I was wondering if there is a built in function or a piece
of code or expression that would assign these rankings automatically in
a new field.

Which brings me to this. I guess I need to learn SQL. Are there any
web resources or books that anyone can recommend? Thanks

max
I created a Continuous form on an employee table query. The two fields
to be displayed were EmployeeID and EmployeeName

I then added a new field. I gave it the name PosIt. It displays the
number as to the rank. In the ControlSource under the datatab I used
=GetPos([EmployeeID])

This calls the function GetPos. This is the code
Private Function GetPos(lngID As Long) As Long
Dim rst As Recordset
Set rst = Me.RecordsetClo ne
rst.FindFirst "EmployeeID = " & lngID
GetPos = Me.RecordsetClo ne.AbsolutePosi tion + 1
rst.Close
Set rst = Nothing
End Function

Would something similar work OK for you? You'd need to modify the above
code and pass the customerID and State (in case the same customer is in
multiple states) to the function.

Oct 8 '06 #2
max wrote:
Let's say I have a table with the following fields. State, Customer,
and Total Sales.
Do yourself a big favor and use only alphanumerics and the underscore for
names. Use the caption property to display something different for the user.
You avoid a lot of bugs that way.
What I want to do is rank each customer by sales
volume by state. i.e. highest sales is ranked 1, second highest ranked
2, etc within each state.
To rank records per group (State), use a correlated subquery:

SELECT (SELECT COUNT(*) + 1
FROM tblCustSales AS C
WHERE (C.State = CS.State AND C.TotalSales CS.TotalSales)) AS RankInState,

CS.State, CS.Customer, CS.TotalSales
FROM tblCustSales AS CS
ORDER BY CS.State, CS.TotalSales DESC;
I guess I need to learn SQL.
Yes, you do if you want to work with relational data to answer questions that
are more complex than can be designed with the QBE grid.
Are there any
web resources or books that anyone can recommend?
Web tutorials will give you the basics, but they don't teach you enough. You
need a book (or a few). "SQL Queries for Mere Mortals" by Michael Hernandez
and John Viescas is a good start.

--
Message posted via AccessMonster.c om
http://www.accessmonster.com/Uwe/For...ccess/200610/1

Oct 8 '06 #3
max
Thanks for your help. I ordered the book you recommended. Most
everything I do involves queries, so I guess it is long overdue to
learn SQL.
Granny Spitz via AccessMonster.c om wrote:
max wrote:
Let's say I have a table with the following fields. State, Customer,
and Total Sales.

Do yourself a big favor and use only alphanumerics and the underscore for
names. Use the caption property to display something different for the user.
You avoid a lot of bugs that way.
What I want to do is rank each customer by sales
volume by state. i.e. highest sales is ranked 1, second highest ranked
2, etc within each state.

To rank records per group (State), use a correlated subquery:

SELECT (SELECT COUNT(*) + 1
FROM tblCustSales AS C
WHERE (C.State = CS.State AND C.TotalSales CS.TotalSales)) AS RankInState,

CS.State, CS.Customer, CS.TotalSales
FROM tblCustSales AS CS
ORDER BY CS.State, CS.TotalSales DESC;
I guess I need to learn SQL.

Yes, you do if you want to work with relational data to answer questions that
are more complex than can be designed with the QBE grid.
Are there any
web resources or books that anyone can recommend?

Web tutorials will give you the basics, but they don't teach you enough. You
need a book (or a few). "SQL Queries for Mere Mortals" by Michael Hernandez
and John Viescas is a good start.

--
Message posted via AccessMonster.c om
http://www.accessmonster.com/Uwe/For...ccess/200610/1
Oct 10 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2968
by: Can | last post by:
I am creating an on-line survey. I want the user to have a list of choices of say 10 items that are radio buttons. They need to rank their preference. They click on preference 1, that option is removed from the top list (choices) and appears below in a list called 'Your preferences', you keep looping until all of your preferences are made....
3
2158
by: bughunter | last post by:
I discover next problem I have view definition with rank() create view vTEST as select c1, c2, c3, ... -- problem area start , rank() over (order by c3) as RNK -- problem area stop from table
6
3083
by: Paul T. Rong | last post by:
Dear all, Here is my problem: There is a table "products" in my access database, since some of the products are out of date and stopped manufacture, I would like to delete those PRODUCTS from the table, but I was not allowed to do that, because "there are records related with those PRODUCTS in other tables (e.g. in table "ORDER_DETAIL").
2
1951
by: dam_fool_2003 | last post by:
Just now I asked a question about signed char and unsigned char. I had the replay very fast by jens.torring and Tim prince. I post via google so now goes my thanking replay. (I must be more precise according to jens.torring's replay) As I was reading the slandered draft C99 about the conversion (6.3) I have interpreted the word conversion...
13
5018
by: Steve Edwards | last post by:
Hi, Given a map: typedef map<long, string, greater<long> > mapOfFreq; Is there a quicker way to find the rank (i.e. index) of the elememt that has the long value of x? At the moment I'm iterating through the map and keeping count of when I hit it.
1
1670
by: volunteer | last post by:
<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="filename.xsl"?> <markers date="20060523"> 04:21:50 PM <marker sn="1" rank="6" name="john" /> <marker sn="2" rank="5" name="mary" /> <marker sn="3" rank="4" name=suzy" /> </markers> How do I sort by and display using etc- some help
2
2939
by: orenlevy1 | last post by:
Hi Everyone. I have a problem that I could not figure out what to do with it. I have a couple of tables and views. All have the same unique ID. When a user try to do a search on our web site we want to try to look in multiple tables and views, Rank the results and aggregate the results back (remove any duplicates). Part of the search is...
0
2390
by: shilpa.vastrad | last post by:
how to calculate rank in selected records in sql server 2000
6
1854
by: canabatz | last post by:
Hello . i got a list of user with ranking . i want to display the first five places where it is not the same user, like that john : is rank 1 john john david : is rank 2 albert : is rank 3 albert
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7922
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8119
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7668
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1209
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.