Results 1 to 12 of 12
  1. #1
    gcgoode is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Aug 2015
    Posts
    31

    Average Rank query

    Right now, I have an output that looks like this:


    Code:
    AvgCost   CompanyID         Policy#
    25              22                    12
    28              23                    12
    35              24                    12
    21              25                    12
    20              22                    20
    15              24                    20
    13              23                    21
    43              24                    21
    Etc.

    I want to know if it is possible to get an output that adds a ranks the CompanyIds by average cost per each policy #.


    I really am just interested in how companyID24 ranks, and I have the query set up where it only outputs Policy#'s that company 24 is ranked in (has sold product in).

    So in the probable case that was difficult to understand, here is what I am wanting.

    Output I have been working to achieve (but having struggles):

    Code:
    AvgCost   CompanyID         Policy#         Rank
    25              22                    12              2
    28              23                    12              3
    35              24                    12              4
    21              25                    12              1
    20              22                    20              2
    15              24                    20              1
    13              23                    21              1
    43              24                    21              2
    Ideal output, that I have no clue on how to do:
    Code:
    AvgCost   CompanyID         Policy#         Rank
    35              24                    12              4
    15              24                    20              1
    43              24                    21              2

    Any help would be greatly appreciated.

    Thank you.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    1 way is:
    to do a make table query, of your avgs sorted 1 to N
    then add [RANK] field as (autonumber). this will instantly set the rankings. 1 thru N.

  3. #3
    gcgoode is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Aug 2015
    Posts
    31
    Quote Originally Posted by ranman256 View Post
    1 way is:
    to do a make table query, of your avgs sorted 1 to N
    then add [RANK] field as (autonumber). this will instantly set the rankings. 1 thru N.
    Wouldn't that just rank the Cost, without regard to the policy#?

  4. #4
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    the records go in whatever sort you give them. If you sort Avg, they will be in Avg order in the table and stay that way when you add the autonumber.
    but if you want different #1 for different companies, then this wont work.

  5. #5
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    here is some code that will scan thru a query, and set the 'ranking' field.
    You provide the query, sorted as needed, the group and the field to write the rank# to.
    If you use various Groups to rank, it resets on that group. So the query must sort by: group, Value (descending)
    give: query, groupField, RankField

    usage:
    RankByGrp "qsTopSales","EmpID","RANK"




    Code:
      'rank items in query, break on GROUPBY
    Public Sub RankByGrp(pvQry, ByVal pvGroupBy, ByVal pvRankFld)
    'pvQry : query name, sorted by GroupBy then value# to rank
    'pvGroupBy : field to group the rankings on.  Rank resets to 1, when the Group changes
    'pvRankFld : fieldname to write the rank#
    
    
    Dim rst   'As Recordset
    Dim lNum As Long
    Dim vPrevGrp, vGrp
    
    
    lNum = 1
    Set rst = CurrentDb.OpenRecordset(pvQry)
    With rst
        While Not .EOF
             vGrp = .Fields(pvRankFld).Value & ""
             If vGrp <> vPrevGrp Then lNum = 1
             
             .Fields(pvRankFld) = lNum
             .Update
             lNum = lNum + 1
             
             .MoveNext
              vPrevGrp = vGrp
        Wend
    End With
    Set rst = Nothing
    End Sub

  6. #6
    gcgoode is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Aug 2015
    Posts
    31
    Thank you for this help. Excuse my ignorance, but I'm having a bit of trouble with how I use the code.

    Do I still need to make my query into a table, add the autonumber field, and then make a query based of that table? And what query name is going in that first position where you have qsTopSales, the first one?

  7. #7
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    like I said, if you are using only 1 rank for all records, use the autonumber trick.
    but
    if you are ranking say, clients in a company, but the data has many companies, then use the code.

    Im still not clear on what you want ranked. policies / company?

  8. #8
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    build a query 'qsTopPolysByCo' with the sort order
    select * from qsAvg order by CompanyID, AvgCost

    put a button on a form
    in the button click event run:

    RankByGrp "qsTopPolysByCo","CompanyID","RANK"

  9. #9
    gcgoode is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Aug 2015
    Posts
    31
    Sorry about the confusion, I just meant I was confused about how to use the code.

    And I want to rank the CompanyID's by my cost field per each Policy#.

  10. #10
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    @ranman
    I think you are missing a .Edit in your VBA example. Also, you might want to add a .MoveFirst

  11. #11
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    MoveFirst is not needed.
    its the default. (it doesnt start in the middle)

  12. #12
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by ranman256 View Post
    MoveFirst is not needed.
    its the default. (it doesnt start in the middle)
    I believe you are correct. That is why I used the word "might". I believe the code there will default to a "Forward Only" recordset which places the cursor at the first record.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Rank Query
    By cbritt927 in forum Queries
    Replies: 2
    Last Post: 06-02-2015, 04:42 PM
  2. Replies: 2
    Last Post: 04-07-2015, 05:34 AM
  3. Query Rank
    By DHIGHTO in forum Queries
    Replies: 14
    Last Post: 01-12-2015, 02:59 PM
  4. Select Rank Based Query
    By trafficman in forum Queries
    Replies: 4
    Last Post: 11-27-2014, 02:24 PM
  5. how to rank using a query in acess 2007
    By BRAYAN RYAN VAN KYAN in forum Queries
    Replies: 1
    Last Post: 08-13-2013, 02:11 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums