View Full Version : Getting minimum or maximum in a group
BigDaddy
03-10-2003, 09:05 PM
Is there a simple way to get the highest numbers from a group of numbers
Ex:
1,3, 7, 5,
If I submitted these 4, the highest is 7. Anyway to return that, similar to a "max()" function in sql?
whammy
03-11-2003, 12:35 AM
Depends, what are you trying to do this in?
One way that comes to mind is to create an array, and as you loop through always store the highest number into a temporary variable... like:
<% @Language="VBScript" %>
<% Option Explicit %>
<%
Function MaxNumber(byVal sNumArray)
Dim myArr, i, iMax
iMax = 0
myArr = Split(sNumArray,",")
For i = 0 to UBound(myArr)
If CInt(myArr(i)) > iMax Then iMax = CInt(myArr(i))
Next
MaxNumber = iMax
End Function
Response.Write(MaxNumber("1,3,5,7,9,27,35,5,3,1"))
%>
:)
P.S. I ran into some weird bugs in VBScript when testing this though - although that's not unusual, as you well know... ;)
BigDaddy
03-11-2003, 03:16 PM
What I was going to do was pull some database queries, and storing the value from each, I would then compare them later.
I was able to figure out a way to use min() in a query, though.
Working on a security project for our intranet. There are 8 different areas, and the employee can be a member of up to 8 different groups. I'm getting all the groups from one database, then querying the database to find the lowest security value that each of those groups has for each area. Originally, I was returning 8 variables for each area type. Using min, I was able to just return the lowest.
After doing that, I then compile an auth_level variable.
1,1,1,2,3,2,1,1
In each area, I check the value of the # in the corresponding position using mid() to determine whether or not to give access, or read only access, or whatever.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.