473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Ranking By Groups

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,
Expand|Select|Wrap|Line Numbers
  1. Region    Area     Sales
  2. R1          A1       100
  3. R1          A2       200
  4. R2          A1       300
  5. R1          A1       400
The output I would like is
Expand|Select|Wrap|Line Numbers
  1. Region    Area     Sales  Rank
  2. R1          A1       100    3
  3. R1          A2       200    2
  4. R2          A1       300    1
  5. R1          A1       400    1
Any help on this is appreciated

Thx
Mar 11 '09 #1
3 1514
ADezii
8,834 Expert 8TB
@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.
Mar 11 '09 #2
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!
Mar 12 '09 #3
ADezii
8,834 Expert 8TB
@ssowjanyau
The following code segment assumes the following:
  1. Your Table Name is tblSales, if it is not, just make the necessary changes in Lines 7, 15, and 16.
  2. The Fields and their Data Types in tblSales are:
    • [Region] - {TEXT}
    • [Area] - {TEXT}
    • [Sales] - {CURRENCY}
    • [Rank] - {INTEGER} - (must add to tblSales)
  3. Execute the following code, your desired Output, (Ranking in the [Rank] Field), should be in tblSales:
    Expand|Select|Wrap|Line Numbers
    1. Dim strSQL As String
    2. Dim MyDB As DAO.Database
    3. Dim rstDistinctRegions As DAO.Recordset
    4. Dim rstSalesByRegion As DAO.Recordset
    5. Dim intRank As Integer
    6.  
    7. strSQL = "Select Distinct tblSales.[Region] From tblSales"
    8.  
    9.  
    10. Set MyDB = CurrentDb
    11. Set rstDistinctRegions = MyDB.OpenRecordset(strSQL, dbOpenSnapshot)
    12.  
    13. With rstDistinctRegions
    14.   Do While Not .EOF
    15.     Set rstSalesByRegion = MyDB.OpenRecordset("Select * From tblSales Where tblSales.[Region] = '" & _
    16.                                 ![Region] & "' Order By tblSales.[Sales] Desc;", dbOpenDynaset)
    17.       Do While Not rstSalesByRegion.EOF
    18.        intRank = intRank + 1
    19.          rstSalesByRegion.Edit
    20.            rstSalesByRegion![Rank] = intRank
    21.          rstSalesByRegion.Update
    22.          rstSalesByRegion.MoveNext
    23.       Loop
    24.         intRank = 0
    25.        Debug.Print
    26.     .MoveNext
    27.   Loop
    28. End With
    29.  
    30. rstSalesByRegion.Close
    31. rstDistinctRegions.Close
    32. Set rstSalesByRegion = Nothing
    33. Set rstDistinctRegions = Nothing
  4. 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)!
Mar 12 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

11
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...
5
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...
1
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...
5
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 ...
6
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...
5
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
0
debasisdas
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...
1
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 ...
0
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 ...
0
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
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,...
0
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...

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.