Hello
I have a access db table where i have region sales by area, I would like to rank the areas within the region by sales amount, - Region Area Sales
-
R1 A1 100
-
R1 A2 200
-
R2 A1 300
-
R1 A1 400
The output I would like is - Region Area Sales Rank
-
R1 A1 100 3
-
R1 A2 200 2
-
R2 A1 300 1
-
R1 A1 400 1
Any help on this is appreciated
Thx
3 1514 @ssowjanyau
I do have a code-based solution that will work, but I'm sure that there must be a better, more efficient, SQL-based approach. Be patient and wait and see what the other more 'SQL Qualified Members' have to say regarding this Thread. If no valid solutions are forthcoming, then I'll post my code-based response, Check in later.
NeoPa 32,556
Expert Mod 16PB
This is possible, but what you're asking is quite unclear.
What are you grouping on? You say Area within Region, yet R1 A1 is listed twice at different ranks in your expected output.
Also, the name of the table would be helpful to know for creating relevant SQL.
Welcome to Bytes!
@ssowjanyau
The following code segment assumes the following: - Your Table Name is tblSales, if it is not, just make the necessary changes in Lines 7, 15, and 16.
- The Fields and their Data Types in tblSales are:
- [Region] - {TEXT}
- [Area] - {TEXT}
- [Sales] - {CURRENCY}
- [Rank] - {INTEGER} - (must add to tblSales)
- Execute the following code, your desired Output, (Ranking in the [Rank] Field), should be in tblSales:
- Dim strSQL As String
-
Dim MyDB As DAO.Database
-
Dim rstDistinctRegions As DAO.Recordset
-
Dim rstSalesByRegion As DAO.Recordset
-
Dim intRank As Integer
-
-
strSQL = "Select Distinct tblSales.[Region] From tblSales"
-
-
-
Set MyDB = CurrentDb
-
Set rstDistinctRegions = MyDB.OpenRecordset(strSQL, dbOpenSnapshot)
-
-
With rstDistinctRegions
-
Do While Not .EOF
-
Set rstSalesByRegion = MyDB.OpenRecordset("Select * From tblSales Where tblSales.[Region] = '" & _
-
![Region] & "' Order By tblSales.[Sales] Desc;", dbOpenDynaset)
-
Do While Not rstSalesByRegion.EOF
-
intRank = intRank + 1
-
rstSalesByRegion.Edit
-
rstSalesByRegion![Rank] = intRank
-
rstSalesByRegion.Update
-
rstSalesByRegion.MoveNext
-
Loop
-
intRank = 0
-
Debug.Print
-
.MoveNext
-
Loop
-
End With
-
-
rstSalesByRegion.Close
-
rstDistinctRegions.Close
-
Set rstSalesByRegion = Nothing
-
Set rstDistinctRegions = Nothing
- Any questions, please feel free to ask. I would still wait for additional Replies, since this is probably not the most efficient solution. I'm sure NeoPa has one already brewing! (LOL)!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Petre Huile |
last post by:
I have designed a site for a client, but they have hired an internet
marketing person to incrase their search engine ranking and traffic.
He wants to put extra-large fonts on every page which will...
|
by: ED |
last post by:
I currently have vba code that ranks employees based on their average
job time ordered by their region, zone, and job code. I currently
have vba code that will cycle through a query and ranks each...
|
by: Joseph Bloch |
last post by:
In all the threads on ranking in Access queries, I've not seen
anything that deals with ranking against a subset of the query
results. What I need to do is take the following query results:
Dept...
|
by: valglad |
last post by:
Hi,
The question below was posted about 4 years ago and noone was able to
answer it back then. I have virtually the same type of problem so would
appreciate if anyone can help.
Thanks
...
|
by: sara |
last post by:
I hope someone can help with this.
Our director wants to have a report that will have the departments
(Retail stores) across the top, stores down the side and the RANKING of
the YTD dept sales...
|
by: Chris |
last post by:
I was wodering if there was a way to rank numbers in a query like this
#'s Rank
100 1
99 2
98 3
98
97 5
96 6
96
|
by: debasisdas |
last post by:
Rank:-assigns A Unique Number For Each Row Starting With 1,except For Rows That Have Duplicate Values,in Which Case The Same Ranking Is Assigned And A Gap Appears In The Sequence For Each Duplicate...
|
by: Paulo |
last post by:
Hi, I need to create a ranking column wich will be the row number... it is
the seller wich most sells...
Ranking Seller Sum
1 Paul 2.212,00
2 Robert ...
|
by: xahlee |
last post by:
I have updated the computing sites popularity ranking, based on both
alexa.com and quantcast.com.
The whole report nicely formatted in HTML is here:
http://xahlee.org/lang_traf/lang_sites.html
...
|
by: Maciej Gawinecki |
last post by:
Hello,
Recently I've interested in quering XML document streams. They can be
queried in keyword- based manner (non-structural queries), as the
authors of the article "Semantic Search over XML...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
| |