473,387 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

MAX value in query

What might be the formula or code to find the max value of 3-6 fields PER RECORD in a query. I have individual vendors offering prices for a number of products. Each product is a new record. I would like a calculated expression in my query design to be BEST COST. BEST COST would be the max value of the 3-6 vendors. I thought of using iif() but I felt there should be better way. Any help?
Nov 17 '06 #1
13 9803
NeoPa
32,556 Expert Mod 16PB
2 options I know of :
1. V V complicated IIF().
2. Call Public Function in a module designed to find max of parameters entered.
Nov 17 '06 #2
Is there no MAX IN (list of fields) command?
Nov 17 '06 #3
NeoPa
32,556 Expert Mod 16PB
I'm pretty sure there's not :(.
We had a thread in here last week about getting the max of three fields and that was handled by IIF() in the end.
Nov 17 '06 #4
MMcCarthy
14,534 Expert Mod 8TB
What might be the formula or code to find the max value of 3-6 fields PER RECORD in a query. I have individual vendors offering prices for a number of products. Each product is a new record. I would like a calculated expression in my query design to be BEST COST. BEST COST would be the max value of the 3-6 vendors. I thought of using iif() but I felt there should be better way. Any help?
It's possible that this is suitable for a crosstab query using your query as a base.

Can you post the full sql statement of your query and I'll have a look.
Nov 17 '06 #5
MMcCarthy
14,534 Expert Mod 8TB
It's possible that this is suitable for a crosstab query using your query as a base.

Can you post the full sql statement of your query and I'll have a look.
Sorry one other thing can you confirm it is the max of the values you are looking for and not the min (lowest value).
Nov 17 '06 #6
PEB
1,418 Expert 1GB
I can offer you 2 functions...

The first one after adaption can calculate max or min of all fields in a query:

Expand|Select|Wrap|Line Numbers
  1. Function min_in_columns(SQL, Start_col, End_col) As Double
  2. Dim mydb As Database
  3. Dim myr As Recordset
  4. Dim i
  5. Dim min_value
  6.  
  7. Set mydb = CurrentDb()
  8. Set myr = mydb.OpenRecordset(SQL)
  9.  
  10. min_value = 0
  11.  
  12. myr.MoveFirst
  13. For i = Start_col To End_col
  14.     If min_value > myr(i) Then
  15.         min_value = myr(i)
  16.     End If
  17. Next i
  18. myr.Close
  19. mydb.Close
  20.  
  21. min_in_columns = min_value
  22. End Function
  23.  
If you want to use it in a query based on table Data with PK ID so you have to type as column in your query:

Min:min_in_columns("SELECT * FROM DATA WHERE ID="+str(ID)+";", 5, 10)

This will give you the minimum from the columns between 6 and 11

And this function is a bit more simple for avoiding the multiple IIF() only:

Expand|Select|Wrap|Line Numbers
  1. Function MinNumb(A, b)
  2.         If A < b Then
  3.             MinNumb = A
  4.         Else
  5.             MinNumb = b
  6.         End If
  7. End Function
  8.  
  9.  
So for 3 fields it should be:
Min: MinNumb(C, MinNumb(A, b))
Nov 18 '06 #7
To confirm...it is the MAX value I am looking for. My sql is:
SELECT Table1.RequestDate, Table1.Symbol, Table1.Cusip, Table1.MLRate, Table1.TotalQty, Total1.TotalNV, Total1.JefRate, Total1.JefQty, Total1.QuadRate, Total1.QuadQty, IIf([QuadRate]>[JefRate],[QuadRate],[JefRate]) AS [Best Rate], [MLRate]*[TotalNV] AS [Current Cost], [Best Rate]*[TotalNV] AS [Best Cost], [Best Cost]-[Current Cost] AS Difference, IIf([Best Rate]=[JefRate],"Jefferies",IIf([Best Cost]=[QuadRate],"Quadriserv","No Change")) AS [Who to Call]
FROM Table1;

As you can see I have begun an embedded IIF() statement. What I would like to do is in my BEST RATE column of my query, I would put some 'equation' that would return the MAX value from JefRate,ML rate and QuadRate. As for the suggestion of the "simple" function", your example showed complete code for 2 variables (a) and (b). If I have 3,4 or 5 variables, would this not just be the same as my embedded IIF() statement?

Thank you again for any help here!!
Nov 20 '06 #8
I also like your function idea. I have never, but have always wanted to create a function. Would I create it in the modules area?
Nov 20 '06 #9
NeoPa
32,556 Expert Mod 16PB
In the VBA window (Alt-F11 from the database), Right-click anywhere in your project and insert a module.
In here type in the Public function you want.
Nov 20 '06 #10
MMcCarthy
14,534 Expert Mod 8TB
In the VBA window (Alt-F11 from the database), Right-click anywhere in your project and insert a module.
In here type in the Public function you want.
In the module just create the function, referencing Vladi's brilliant! idea.

Expand|Select|Wrap|Line Numbers
  1. Function MaxNumb(A, b)
  2.         If A > b Then
  3.             MaxNumb = A
  4.         Else
  5.             MaxNumb = b
  6.         End If
  7. End Function
Then in query

SELECT MaxNumb(C,MaxNumb(A, B)) As MaxNo ....

Get the idea?
Nov 20 '06 #11
Yes, I did it and it worked! I actually changed the function given to work for more than 2 items! Thanks for all the support!
Nov 20 '06 #12
MMcCarthy
14,534 Expert Mod 8TB
Yes, I did it and it worked! I actually changed the function given to work for more than 2 items! Thanks for all the support!
Using your initiative, that's what I like to see. If you want you can post the function you're currently using for the benefit of future searches on this thread.

Mary
Nov 20 '06 #13
NeoPa
32,556 Expert Mod 16PB
I was just writing a recursive function to do it for any number of parameters when I realised VBA doesn't support passing on all the extra parameters in the 'ParamArray' to another function call :(.

(Killer - please contradict - I hope there really is a way).
Nov 20 '06 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Shufen | last post by:
Hi, I'm a newbie that just started to learn python, html and etc. I have some questions to ask and hope that someone can help me on. I'm trying to code a python script (with HTML) to get...
20
by: | last post by:
If I need to check if a certain value does exist in a field, and return either "yes" or "not" which query would be the most effestive?
7
by: rickcheney | last post by:
I just changed my Access 2002 database to a SQL Server ADP project. I had a form where the user entered a value into a text box and when a command button on the form was clicked a Report was...
1
by: Stuart E. Wugalter | last post by:
Hello. No one in the queries newgroup answered this question. I hope someone here can. Thank you. Stuart The following Update Query allows my users to substitute a numerical value for the...
1
by: Ken | last post by:
I wrote a function to use in queries that takes a date and adds or subtracts a certain length time and then returns the new value. There are times when my function needs to return Null values. ...
3
by: thomas goodwin | last post by:
I have a query which asks for a parameter value to execute it. To see the results I have to: a) click on the query -- the "Enter Parameter Value" window pops up. b) enter the parameter value c)...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
2
by: | last post by:
Hi everyone, I have a form with a combo box on it. When you select a value (a PO#) from the combo box, the bound field is the indexID of the selected PO. On the same form, I have a text box...
0
by: tania | last post by:
i have this table in my database: CREATE TABLE FILM( F_ID INT(5) NOT NULL AUTO_INCREMENT, F_TITLE VARCHAR(40) NOT NULL, DIRECTOR_FNAME VARCHAR(20) NOT NULL, DIRECTOR_LNAME VARCHAR(20) NOT NULL,...
10
by: Brad Baker | last post by:
I have an asp.net/csharp application that requires a particular variable to work properly. This variable is usually passed via a query string in the URL when the application is first run but under...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.