Solved

Rank order fields in record (Access)

Posted on 2009-04-10
8
457 Views
Last Modified: 2013-11-27
Hello, everyone.  Yet another Access/DAO/data question for the masses....

I need to determine rank-order data in a number of fields within a specific record (Access 2003 database).  Actually, I need to find the top 3 highest values within these fields.  Would be easy if these values were in columns (could pull "TOP 3"), but as they fall within fields within the record...how?

I'm actually pulling this data out of Access into an Excel spreadsheet using DAO.  I've been toying around with some methods, but they end up long and drawn out.  I have current written a Sub in Access to "transpose" these fields into rows in a "work" table to pull what I need.  Example of the table I have:
VAR   MR1   MR2   MR3  MR4 .... MR12  (column headings)
TOT    25       15      30     10          3
BUS    10                 15     30         40
LOC    5         17      33                  12
(this is only a small section of the table, there are other fields around these, but these are the ones I need to work with - their will be many more lines)

How, if possible, in "memory" (a recordset object, array, or some such) can I determine the highest three scores and, if possible, the columns/fields they are in "MR#" ?  The results will be used to color-shade the corresponding cells in the Excel spreadsheet.

The Sub I've written spits out a "work" table that looks like:
LineNum   TOT    BUS   LOC    (col headings/field names)
      1          25       10      5
      2          15                17
      3          30        15     33
      4          10        30
      ...          
      12         3         40     12

...so this way, I"m assuming, I can pull the "TOP 3" using SQL in a DAO recordset definition to get what I need (any way to also pull the LineNumber along with the TOP 3 scores?).

Would be great if this can all be done with the recordset object or an array w/o having to resort to another table...

Thank everyone, for any ideas....
0
Comment
Question by:CThomp2005
  • 5
  • 3
8 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 24115511
? TOP 3 of what?
from your original table, which are the TOP 3?

0
 
LVL 3

Author Comment

by:CThomp2005
ID: 24115602
Sorry, capricorn1, I guess I didn't explain that clearly...

For each original row/record, the highest three values of all the MR fields (1-12)...  So, for the original TOT line, I would get back "30" "25" and "15".  Preferably, I could also get the fields they're in ("3" "1" "2" or even "MR3" "MR1" "MR2").  Ordering of the returned top 3 is not necessary, just need to find the highest 3 values.

I have a sense this is "simple", but just can't get my head wrapped around it.  I'm overthinking it, I think..
0
 
LVL 119

Assisted Solution

by:Rey Obrero
Rey Obrero earned 100 total points
ID: 24115688
using your "work" table (since you already have it)

select top 3 lineNum,tot
from worktable
order by tot desc
0
 
LVL 3

Author Comment

by:CThomp2005
ID: 24116233
Thanks much for the response, capricorn1....

A couple of questions...  How could I go about doing this without using the work table?  Concern (which I've run into before) is that the data gets updated periodically, and if I forget to run the Sub to recreate the work table, I deal with old data.  Any way to do this directly in a rs or array?

Next... I see the sql you threw out - along my same lines of thinking, except that I'm not horribly familiar with the "top 3" clause (know about it - never actually used it).  By doing "top 3" against both LineNum and Tot, will that (in some way) pull a "top 3" for LineNum, also?  Just curious here, what would (small example, if poss) would the resultant recordset/array look like after this (before it hits the "order by")?

Thanks very much.  I'll be keeping tabs on this thread pretty closely as I'm at this point in my programming...so responses should be quick...
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 119

Expert Comment

by:Rey Obrero
ID: 24116345
you have to do this using VBA codes in a user define function,
create a function, something like this

function getMax(paramarray args()) as double
dim vMax as double, j
vMax=args(0)
for j=1 to ubound(args)
  if vMax < args(j) then
       vMax=args(j)
  end if
next
getmax=vMax

end function

to use in a query

select VAR,getMax([MR1],[MR2],....[MRn])

that will get you the max value for each row

just tweak it to get three values



0
 
LVL 3

Author Comment

by:CThomp2005
ID: 24116550
OK...I understand where you're going with this...  I get the "select...".  What I'm not sure about is how to "tweak it to get three values"... How would I tweak to get the next two values (2nd and 3rd) ?

Sorry to be dense about this...  Kinda thought there was a way to just read into an array and sort the array descending, and pull out the first three elements...or same with a rs...

0
 
LVL 3

Author Comment

by:CThomp2005
ID: 24117046
Moving on...

After playing around on my end, I've decided to stick with the "use work table and pull top 3 via SQL" trick in your first response...

A slightly off-topic question, considering the solution I'm going with...  From Excel VBA, how can I "run" the Sub that creates the work table?  Can it be done without having the db open?  Theory being, if I can run the code to create the table prior to pulling the top3 lines out, I know I'll be working with the most current data (in the work table).

Thanks again.
0
 
LVL 3

Accepted Solution

by:
CThomp2005 earned 0 total points
ID: 24143189
As a final followup....  as an alternative, I've also tracked down some code to run a sort on an array created by stepping through these fields...  Sooo...

for x = 1 to 12
  ary(x) = .Fields("MR" & x)
next x
....to create the array, then pass it to the "bubble sort" code (set up as a function) to sort it descending.  A subsequent routine steps through the "top" three elements (ary(1) through ary(3)) to pull out the values I need.

Not sure which I'll end up using....in the end...
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article explains all about SQL Server Piecemeal Restore with examples in step by step manner.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now