Full Version: finding the maximum value among n columns in a row using queries
UtterAccess Discussion Forums > Microsoft® Access > Access Queries
aruhn
Hi all,
suppose if i have fields like a,b,c,d..etc..of numeric data type .. in a table , and
i want to store the biggest value among these fields in a new field...what do i do ?
i want this operation done using queries. your valuable suggestions r welcome !
mishej
Welcome to UtterAccess!

This is a common request and it usually indicates an error in database design. Since these values can be compared they most likely represent the same attribute and should all be stored under the same field name (but different rows) in the database.

Because of this concept there aren't good built-in tools (in SQL for example) to handle this request.

Access provides the ability to VBA functions in SQL so you can use the IIF() function to compare two values and take action based on a comparison:

You could use a UNION query to do this:
CODE
SELECT TOP 1 FROM
(SELECT MAX(A) FROM yourTable
  UNION ALL
  SELECT MAX(B) FROM yourTable
  UNION ALL
  SELECT MAX(C) FROM yourTable
  UNION ALL
  SELECT MAX(D) FROM yourTable
) ORDER BY 1
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.