Find Minimum number in access

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have 8 columns in access with different numbers. In the 9th column, how can
I write a formula in update query to populate the minimum number in the
previous 8 columns (in the same row) and have it ignor the 0 or blanks?

Thanks a lot!!
 
You shouldn't. There's no reason to store that information. It can (and
should) be calculated in a query. In that way, you don't have to worry if
one of the 8 fields gets updated without the minimum getting recalculated.

Write a function that accepts 8 variants (in case any of the fields can be
Null). Have it return the minimum of the 8 values, and use that function as
a computed field in a query. Use the query wherever you would otherwise have
used the table.
 
I have 8 columns in access with different numbers. In the 9th column, how can
I write a formula in update query to populate the minimum number in the
previous 8 columns (in the same row) and have it ignor the 0 or blanks?

Thanks a lot!!

Your table should NOT contain such a field.

Storing derived data such as this in your table accomplishes
three things: it wastes disk space; it wastes time (almost
any calculation will be MUCH faster than a disk fetch); and
most importantly, it risks data corruption. If one of the
underlying fields is subsequently edited, you will have data
in your table WHICH IS WRONG, and no automatic way to detect
that fact.

For that matter, having eight fields across and needing to treat them
all in parallel suggests that you have a one to many (one to eight to
be precise) relationship incorrectly embedded in a single record.

You'll need some VBA code to parse the eight fields and find the least
of them. But I'd STRONGLY suggest either doing this in Excel, as it's
really a spreadsheet structure, or properly normalizing your table!


John W. Vinson[MVP]
 
Jason said:
I have 8 columns in access with different numbers. In the 9th column,
how can I write a formula in update query to populate the minimum
number in the previous 8 columns (in the same row) and have it ignor
the 0 or blanks?

Thanks a lot!!

First it sounds like you have a poor table design. Likely those eight
fields should be normalized into another related table or just moved to a
spread sheet in Excel.

Second, in a database you normally should not store results of
calculations or combinations, you recompute or re-combine them as needed.
 
John said:
You'll need some VBA code to parse the eight fields and find the least
of them

Other advice: 'Write a function that accepts 8 variants..' Am I missing
something or can this design flaw be flattened out using a dervied
table and the desired minimum be found using a single query (perhaps in
a VIEW) e.g.

CREATE TABLE TEST (
ID INT NOT NULL PRIMARY KEY,
col1 INT,
col2 INT,
col3 INT
)
;

SELECT DT1.ID,
MIN(DT1.all_cols) AS min_col_value
FROM (
SELECT ID, col1 AS all_cols
FROM TEST
UNION
SELECT ID, col2 AS all_cols
FROM TEST
UNION
SELECT ID, col3 AS all_cols
FROM TEST
) AS DT1
GROUP BY DT1.ID
;

Jamie.

--
 
onedaywhen said:
Other advice: 'Write a function that accepts 8 variants..' Am I missing
something or can this design flaw be flattened out using a dervied
table and the desired minimum be found using a single query (perhaps in
a VIEW) e.g.

CREATE TABLE TEST (
ID INT NOT NULL PRIMARY KEY,
col1 INT,
col2 INT,
col3 INT
)
;

SELECT DT1.ID,
MIN(DT1.all_cols) AS min_col_value
FROM (
SELECT ID, col1 AS all_cols
FROM TEST
UNION
SELECT ID, col2 AS all_cols
FROM TEST
UNION
SELECT ID, col3 AS all_cols
FROM TEST
) AS DT1
GROUP BY DT1.ID

You're right that it can be at least partially done that way. I say
partially because you'd need to join the results of that query back to the
original query in order to get the desired format (a row with the 8 numbers
and the minimum as an additional field). It may well be that it would be
more efficient your way, though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top