Solved

Is it possible to select a minimum and maximum value from multiple fields in Access 2010?

Posted on 2014-10-16
7
1,280 Views
Last Modified: 2014-10-16
I have eight fields that contain counts or percentages of loans made by institution and need to be able to determine which of the these fields has the lowest and highest values.  Ideally, I would use a function like Min([field1], [field2], [field3], etc) to determine which field has the minimum value but no such functions exists in Access 2010.  Each field contains values for a specific category so the values in all fields cannot be combined.  The goal is to show, by institution, which fields have the lowest and highest values.

If someone can suggest a fairly easy way to determine the minimum or maximum value for a group of fields I would appreciate the advice.
0
Comment
Question by:Liberty4all
7 Comments
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40385206
You can do like this .


select institution, min(value)
from 
(
select institution, 'Field1',min(field1) as value
from table
group by institution
union all 
select institution, 'Field2',min(field2) as value
from table
group by institution
union all
.
.
.
select institution, 'Field8',min(field8) as value
from table
group by institution
)a
group by institution 

Open in new window

0
 

Author Comment

by:Liberty4all
ID: 40385339
The approach you recommend would be useful if it was necessary to calculate a minimum or maximum value for each field.  In my case there is only one value per field for each institution so there is no need to calculate a minimum or maximum value for the field itself .  The query results may theoretically resemble the following.  Each number represents the value of each field.  The sort is by institution.

Institution 2 (field1), 5 (field2), 4 (field3), 7 (field4), 0 (field5), 9 (field6), 6 (field7), 10 (field8)  

What I need is the ability to identify the minimum and maximum value identified for each institution from the eight different fields.  In the above example, the minimum value is from field 5 and the maximum is from field 8.  I want to add a column to the query to calculate the minimum and maximum value of the eight fields.
0
 
LVL 35

Accepted Solution

by:
PatHartman earned 500 total points
ID: 40385358
no such functions exists in Access 2010
Nor will you ever see one in any tool that works with relational data.  Functions such as this are used in Spreadsheets because they are not normalized relational tables.  When you have multiple columns like this that occur in a single row, you have a repeating group and a repeating group violates first normal form.  Hence, no support for repeating groups.

If you're not too far down the development road, the best solution is to normalize the table.  This would involve creating a separate table to hold the "many-side" items.

The query suggested by samO4fun normalizes the table on the fly.

If the repeating group is a result of a crosstab, you can modify the crosstab to get the min and max for you.  Generally the crosstab wizard creates only a single Row Heading column and it does a Sum.  I took a query and added a min and max in addition to the generated sum.
 crosstab
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 49

Expert Comment

by:Gustav Brock
ID: 40385413
You can modify the function below to accept currency and not dates.
Then:

curMin = CurMin([Field1], [Field2], [Field3], [Field4])
Public Function DateMax(ParamArray avarDates() As Variant) As Date

' Return maximum date/time Value of elements in
' array avarDates().
' If no elements of array avarDates() are dates,
' Value of cdatEmpty is returned.
'
' 2003-09-30. Cactus Data ApS, CPH.

  ' Return Value for an empty array.
  Const cdatEmpty As Date = #1/1/100#
  
  Dim varDate     As Variant
  Dim varDateMax  As Variant
    
  For Each varDate In avarDates()
    If IsDate(varDate) Then
      If VarType(varDate) <> vbDate Then
        varDate = CDate(varDate)
      End If
      If varDate > varDateMax Then
        varDateMax = varDate
      End If
    End If
  Next
  
  If IsEmpty(varDateMax) Then
    varDateMax = cdatEmpty
  End If
  
  DateMax = varDateMax
  
End Function

Open in new window

/gustav
0
 

Author Comment

by:Liberty4all
ID: 40385420
Pat,

The values that appear in fields 1 through 8 are determined using queries that either sum the count of all records or calculate a percentage.  You are correct in that my values come from a crosstab query and tried the examples you suggested.  They do work but the caveat is that null values are ignored (probably as expected), and if there are multiple columns with identical values the first field with the same value is identified as either minimum or maximum.  This requires that one be attentive to what is happening but I'm guessing this approach is the best option based on your comments.  Thanks for providing it.
0
 

Author Comment

by:Liberty4all
ID: 40385484
Gustav,

Thanks for your code example.  For my purposes Pat's recommendation is better suited to my needs but I will keep your example for possible future use.
0
 
LVL 35

Expert Comment

by:PatHartman
ID: 40385636
Access functions ignore nulls.  So if you average:

3, null, 3 you get 3
but
3, 0, 3 gives you 2

So, you need to control whether a column returns 0 or null to get the result you want.  In almost all cases, I have numeric values default to null because I want the first result.  If I have no value, I don't want the record included in the aggregation.  Once someone provides a value, if that value is 0, it has meaning and so should be included.

As to which column Access chooses when there are duplicate values - at least it is consistent.  I'm not sure you can ask for more.  You would need to write a custom function to get a different result but what would you want it to be?
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add records to a form to a table 11 39
Access report groups with sums 5 26
deduplicating based on criteria 2 21
Dirty form - conditional formatting 5 21
This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

770 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