Min Across columns (1 Viewer)

Tskutnik

Registered User.
Local time
Today, 16:58
Joined
Sep 15, 2012
Messages
229
Hi All, A simple question I'm stuck on. Every Answer I can fin has 200 lines of code. I dont need or want code, just the correct syntax for the following.

IN each row in a table, I want to find the Max value across the following three columns [Col1], [Col2], [Col3]

Something like Max([Col1], [Col2], [Col3]) I'd think but I cant get it.

Thanks for the help
 

Josef P.

Well-known member
Local time
Today, 22:58
Joined
Feb 2, 2023
Messages
826
Code:
select
   YourID,
   IIF(Col1 > Col2, iif(col1 > col3, col1, col3), iif(col2 > col3, col2, col3)) as MaxValue
from
   YourTable
.. without Null check!
or

SQL:
select YourID, Max(ColX) as MaxValue
from (
   select YourID, Col1 as ColX from YourTable
   union all
   select YourID, Col2 as ColX from YourTable
   union all
   select YourID, Col3 as ColX from YourTable
) As X
group by
    YourID

Would that be an argument for normalization? ;)
 

GPGeorge

Grover Park George
Local time
Today, 13:58
Joined
Nov 25, 2004
Messages
1,873
"Would that be an argument for normalization? "

That would be a "Yes."
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 16:58
Joined
Feb 19, 2002
Messages
43,281
Something like Max([Col1], [Col2], [Col3]) I'd think but I cant get it.
Because you are using a relational database and expecting it to work like a spreadsheet.

I dont need or want code

The solution is to do some reading on normalization so you can create the tables correctly. Then the solution will be a simple query. No code required. However, if you choose to not normalize your schema, then you should prepare yourself to become proficient in VBA because you'll be writing a lot of it.

Just a hint, if you find yourself having to suffix a column name because you have duplicates, you have created a repeating group and instead of having a string of columns, you need a child table with in this case three rows per main form record.
 
Last edited:

sxschech

Registered User.
Local time
Today, 13:58
Joined
Mar 2, 2010
Messages
793
Not to wade into the normalization issue, as there are some cases when reporting may need to adjust data or receiving a download from a third party that needs to be refined in order to import into a normalized schema, here are functions from Allen Browne that may work for your situation.

Code:
Function MaxOfList(ParamArray varValues()) As Variant
'http://allenbrowne.com/func-09.html
Dim i As Integer        'Loop controller.
    Dim varMax As Variant   'Largest value found so far.

    varMax = Null           'Initialize to null

    For i = LBound(varValues) To UBound(varValues)
        If IsNumeric(varValues(i)) Or IsDate(varValues(i)) Then
            If varMax >= varValues(i) Then
                'do nothing
            Else
                varMax = varValues(i)
            End If
        End If
    Next

    MaxOfList = varMax
End Function

Code:
Function MinOfList(ParamArray varValues()) As Variant
    Dim i As Integer        'Loop controller.
    Dim varMin As Variant   'Smallest value found so far.

    varMin = Null           'Initialize to null

    For i = LBound(varValues) To UBound(varValues)
        If IsNumeric(varValues(i)) Or IsDate(varValues(i)) Then
            If varMin <= varValues(i) Then
                'do nothing
            Else
                varMin = varValues(i)
            End If
        End If
    Next

    MinOfList = varMin
End Function

Example:
Code:
SELECT Docs.FileType, Docs.DocumentNo, Docs.DocumentTitle, Docs.DateModified, Docs.RevisionDate, Docs.DateAppended, MaxOfList([datemodified],[revisiondate],[dateappended]) AS MaxDate, MinOfList([datemodified],[revisiondate],[dateappended]) AS MinDate
FROM Docs;
 

sxschech

Registered User.
Local time
Today, 13:58
Joined
Mar 2, 2010
Messages
793
Sorry for adding, only thought in case someone else read the thread looking for ways on how and might be able to find the code option of use.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 21:58
Joined
Feb 19, 2013
Messages
16,616
You can normalise the view with a union query. Not very efficient but no code
 

Users who are viewing this thread

Top Bottom