472,342 Members | 1,282 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

How to calc the greatest column value for each record?

Suppose I have a table t with columns id, col1, col2, col3, col4, col5, col6 all numeric. I want to query the table and for each value of col<x> in the resultset I want to identify the largest value in each of the columns. For example, with the following dataset:

id, col1, col2, col3, col4, col5, col6
1,2,3,4,5,6,7
2,8,6,5,4,3,2
3,2,3,9,4,2,1

I want a query to generate the following output:

1,7
2,8
3,9

In other databases I might use the "greatest" function eg: in mySQL;

select id, greatest( col1, col2, col3, col4, col5, col6 );

I've checked the help, googled and searched through a book to no avail. Is there a simple SQL based solution to this problem in MS access?

Thanks in advance,

Mat.
May 22 '06 #1
7 4071
* Mathew Butler:
Suppose I have a table t with columns id, col1, col2, col3, col4, col5,
col6 all numeric. I want to query the table and for each value of col<x>
in the resultset I want to identify the largest value in each of the
columns. For example, with the following dataset:

id, col1, col2, col3, col4, col5, col6
1,2,3,4,5,6,7
2,8,6,5,4,3,2
3,2,3,9,4,2,1

I want a query to generate the following output:

1,7
2,8
3,9

In other databases I might use the "greatest" function eg: in mySQL;

select id, greatest( col1, col2, col3, col4, col5, col6 );

I've checked the help, googled and searched through a book to no avail.
Is there a simple SQL based solution to this problem in MS access?

Thanks in advance,

Mat.


MAX is the SQL standard function.

--
Randy Harris
tech at promail dot com
I'm pretty sure I know everything that I can remember.
May 22 '06 #2
"Mathew Butler" <ma***********@logicacmg.com> wrote in
news:11***************@ernani.logica.co.uk:
Suppose I have a table t with columns id, col1, col2, col3,
col4, col5, col6 all numeric. I want to query the table and
for each value of col<x> in the resultset I want to identify
the largest value in each of the columns. For example, with
the following dataset:

id, col1, col2, col3, col4, col5, col6
1,2,3,4,5,6,7
2,8,6,5,4,3,2
3,2,3,9,4,2,1

I want a query to generate the following output:

1,7
2,8
3,9

In other databases I might use the "greatest" function eg: in
mySQL;

select id, greatest( col1, col2, col3, col4, col5, col6 );

I've checked the help, googled and searched through a book to
no avail. Is there a simple SQL based solution to this problem
in MS access?

Thanks in advance,

Mat.


You could write a greatest() function, which is a kludge,
because like data is supposed to go into one column with
multiple rows, otherwise it's not normalized, or you could
create a union query of the ID, a subID, and the value, creating
a virtual table properly set up.

SELECT ID, 1 as COLID, col1 as theValue from table
UNION
SELECT ID, 2 as COLID, col2 from table
UNION
SELECT ID, 3 as COLID, col3 from table
UNION
SELECT ID, 4 as COLID, col4 from table
UNION
SELECT ID, 5 as COLID, col5 from table
UNION
SELECT ID, 16as COLID, col6 from table

Then use that as a source to a summation query

SELECT ID, COL, Max(theValue) from Query GROUP ON
ID, COLID

--
Bob Quintal

PA is y I've altered my email address.
May 22 '06 #3
* Randy Harris:
* Mathew Butler:
Suppose I have a table t with columns id, col1, col2, col3, col4,
col5, col6 all numeric. I want to query the table and for each value
of col<x> in the resultset I want to identify the largest value in
each of the columns. For example, with the following dataset:

id, col1, col2, col3, col4, col5, col6
1,2,3,4,5,6,7
2,8,6,5,4,3,2
3,2,3,9,4,2,1

I want a query to generate the following output:

1,7
2,8
3,9

In other databases I might use the "greatest" function eg: in mySQL;

select id, greatest( col1, col2, col3, col4, col5, col6 );

I've checked the help, googled and searched through a book to no
avail. Is there a simple SQL based solution to this problem in MS access?

Thanks in advance,

Mat.


MAX is the SQL standard function.


Bob's right. I was careless with my reply. This is not trivial with SQL.
Best bet is to create you own "greatest" function.

Public Function Greatest(c1, c2, c3, c4, c5, c6)

Greatest = c1
If c2 > Greatest Then Greatest = c2
If c3 > Greatest Then Greatest = c3
If c4 > Greatest Then Greatest = c4
If c5 > Greatest Then Greatest = c5
If c6 > Greatest Then Greatest = c6

End Function

Since the fields are all numeric, you need not worry about nulls. Sorry
for the poor answer.

--
Randy Harris
tech at promail dot com
I'm pretty sure I know everything that I can remember.
May 23 '06 #4
Many thanks.

I'd come to the similar conclusions, but I'd hoped for an existing function.

"Bob Quintal" <rq******@sympatico.ca> wrote in message
news:Xn**********************@207.35.177.135...
"Mathew Butler" <ma***********@logicacmg.com> wrote in
news:11***************@ernani.logica.co.uk:
Suppose I have a table t with columns id, col1, col2, col3,
col4, col5, col6 all numeric. I want to query the table and
for each value of col<x> in the resultset I want to identify
the largest value in each of the columns. For example, with
the following dataset:

id, col1, col2, col3, col4, col5, col6
1,2,3,4,5,6,7
2,8,6,5,4,3,2
3,2,3,9,4,2,1

I want a query to generate the following output:

1,7
2,8
3,9

In other databases I might use the "greatest" function eg: in
mySQL;

select id, greatest( col1, col2, col3, col4, col5, col6 );

I've checked the help, googled and searched through a book to
no avail. Is there a simple SQL based solution to this problem
in MS access?

Thanks in advance,

Mat.


You could write a greatest() function, which is a kludge,
because like data is supposed to go into one column with
multiple rows, otherwise it's not normalized, or you could
create a union query of the ID, a subID, and the value, creating
a virtual table properly set up.

SELECT ID, 1 as COLID, col1 as theValue from table
UNION
SELECT ID, 2 as COLID, col2 from table
UNION
SELECT ID, 3 as COLID, col3 from table
UNION
SELECT ID, 4 as COLID, col4 from table
UNION
SELECT ID, 5 as COLID, col5 from table
UNION
SELECT ID, 16as COLID, col6 from table

Then use that as a source to a summation query

SELECT ID, COL, Max(theValue) from Query GROUP ON
ID, COLID

--
Bob Quintal

PA is y I've altered my email address.

May 23 '06 #5
Very helpful - thanks.

I'd really hoped for an existing function - but you have provided an
impelmention - thanks for sharing!

"Randy Harris" <pl****@send.no.spam> wrote in message
news:6D*******************@newssvr12.news.prodigy. com...
* Randy Harris:
* Mathew Butler:
Suppose I have a table t with columns id, col1, col2, col3, col4,
col5, col6 all numeric. I want to query the table and for each value
of col<x> in the resultset I want to identify the largest value in
each of the columns. For example, with the following dataset:

id, col1, col2, col3, col4, col5, col6
1,2,3,4,5,6,7
2,8,6,5,4,3,2
3,2,3,9,4,2,1

I want a query to generate the following output:

1,7
2,8
3,9

In other databases I might use the "greatest" function eg: in mySQL;

select id, greatest( col1, col2, col3, col4, col5, col6 );

I've checked the help, googled and searched through a book to no
avail. Is there a simple SQL based solution to this problem in MS access?
Thanks in advance,

Mat.


MAX is the SQL standard function.


Bob's right. I was careless with my reply. This is not trivial with SQL.
Best bet is to create you own "greatest" function.

Public Function Greatest(c1, c2, c3, c4, c5, c6)

Greatest = c1
If c2 > Greatest Then Greatest = c2
If c3 > Greatest Then Greatest = c3
If c4 > Greatest Then Greatest = c4
If c5 > Greatest Then Greatest = c5
If c6 > Greatest Then Greatest = c6

End Function

Since the fields are all numeric, you need not worry about nulls. Sorry
for the poor answer.

--
Randy Harris
tech at promail dot com
I'm pretty sure I know everything that I can remember.

May 23 '06 #6
I've included a function below that accepts a list punctuated by semi-colons and returns the largest value in the list. It seems to work fine with string values, integer values and non-integer values. A list contain numeric and non-numeric values is processed as a list of strings.

I don't know how practical it is for your situation, but it would work. It would require that you call the function in a query and concatenate the columns together as the argument to the function:

Select ID, GreatestInList(col1 & ";" & col2 & ";" & col3 & ";" & col4 & ";" & col5 & ";" & col6) As LargestValue

It doesn't matter how many columns you string together, or in what order, or anything else. As long as the list consists of values separated by semicolons it should work.

The function is shown below. Like I said, I don't know if this is a practical way for you, but it was an interesting challenge and wound up being not much code.

Public Function GreatestInList(ByVal varList As Variant) As Variant

Dim aList() As String
Dim l As Long
Dim greatest As Variant
Dim AllNumeric As Boolean
Dim AllInteger As Boolean

GreatestInList = Null
If IsNull(varList) = True Then
Exit Function
End If
If InStr(1, varList, ";") = 0 Then
GreatestInList = varList
Exit Function
End If
aList = Split(varList, ";")
greatest = aList(LBound(aList))
AllNumeric = True
AllInteger = True
For l = LBound(aList) To UBound(aList)
If IsNumeric(aList(l)) = False Then
AllNumeric = False
AllInteger = False
Exit For
End If
If AllNumeric = True Then
If InStr(1, aList(l), ".") = True Then
AllInteger = False
Exit For
End If
End If
Next l
For l = (LBound(aList) + 1) To UBound(aList)
If AllNumeric = False Then
'Since not all values are numeric, compare as strings
If aList(l) > greatest Then
greatest = aList(l)
End If
Else
If AllInteger = True Then
If CLng(aList(l)) > CLng(greatest) Then
greatest = aList(l)
End If
Else
If CDbl(aList(l)) > CDbl(greatest) Then
greatest = aList(l)
End If
End If
End If
Next l
GreatestInList = greatest
Exit Function

End Function

"Mathew Butler" <ma***********@logicacmg.com> wrote in message news:11***************@ernani.logica.co.uk...
Suppose I have a table t with columns id, col1, col2, col3, col4, col5, col6 all numeric. I want to query the table and for each value of col<x> in the resultset I want to identify the largest value in each of the columns. For example, with the following dataset:

id, col1, col2, col3, col4, col5, col6
1,2,3,4,5,6,7
2,8,6,5,4,3,2
3,2,3,9,4,2,1

I want a query to generate the following output:

1,7
2,8
3,9

In other databases I might use the "greatest" function eg: in mySQL;

select id, greatest( col1, col2, col3, col4, col5, col6 );

I've checked the help, googled and searched through a book to no avail. Is there a simple SQL based solution to this problem in MS access?

Thanks in advance,

Mat.
May 25 '06 #7
For consistency and reliability, this line:

If aList(l) > greatest Then

should have been written as this:

If cstr(aList(l)) > cstr(greatest) Then

I don't think it would fail for any reason without the cstr(), but since explicit is better than implicit any day, it should be in there.

"Rick Wannall" <cw******@yahoo.com> wrote in message news:Zf******************@newssvr12.news.prodigy.c om...
I've included a function below that accepts a list punctuated by semi-colons and returns the largest value in the list. It seems to work fine with string values, integer values and non-integer values. A list contain numeric and non-numeric values is processed as a list of strings.

I don't know how practical it is for your situation, but it would work. It would require that you call the function in a query and concatenate the columns together as the argument to the function:

Select ID, GreatestInList(col1 & ";" & col2 & ";" & col3 & ";" & col4 & ";" & col5 & ";" & col6) As LargestValue

It doesn't matter how many columns you string together, or in what order, or anything else. As long as the list consists of values separated by semicolons it should work.

The function is shown below. Like I said, I don't know if this is a practical way for you, but it was an interesting challenge and wound up being not much code.

Public Function GreatestInList(ByVal varList As Variant) As Variant

Dim aList() As String
Dim l As Long
Dim greatest As Variant
Dim AllNumeric As Boolean
Dim AllInteger As Boolean

GreatestInList = Null
If IsNull(varList) = True Then
Exit Function
End If
If InStr(1, varList, ";") = 0 Then
GreatestInList = varList
Exit Function
End If
aList = Split(varList, ";")
greatest = aList(LBound(aList))
AllNumeric = True
AllInteger = True
For l = LBound(aList) To UBound(aList)
If IsNumeric(aList(l)) = False Then
AllNumeric = False
AllInteger = False
Exit For
End If
If AllNumeric = True Then
If InStr(1, aList(l), ".") = True Then
AllInteger = False
Exit For
End If
End If
Next l
For l = (LBound(aList) + 1) To UBound(aList)
If AllNumeric = False Then
'Since not all values are numeric, compare as strings
If aList(l) > greatest Then
greatest = aList(l)
End If
Else
If AllInteger = True Then
If CLng(aList(l)) > CLng(greatest) Then
greatest = aList(l)
End If
Else
If CDbl(aList(l)) > CDbl(greatest) Then
greatest = aList(l)
End If
End If
End If
Next l
GreatestInList = greatest
Exit Function

End Function

"Mathew Butler" <ma***********@logicacmg.com> wrote in message news:11***************@ernani.logica.co.uk...
Suppose I have a table t with columns id, col1, col2, col3, col4, col5, col6 all numeric. I want to query the table and for each value of col<x> in the resultset I want to identify the largest value in each of the columns. For example, with the following dataset:

id, col1, col2, col3, col4, col5, col6
1,2,3,4,5,6,7
2,8,6,5,4,3,2
3,2,3,9,4,2,1

I want a query to generate the following output:

1,7
2,8
3,9

In other databases I might use the "greatest" function eg: in mySQL;

select id, greatest( col1, col2, col3, col4, col5, col6 );

I've checked the help, googled and searched through a book to no avail. Is there a simple SQL based solution to this problem in MS access?

Thanks in advance,

Mat.
May 25 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: zaceti | last post by:
I'm new to MySQL and I am having a problem selecting the highest valued date/time for a particular day. Here is the table structure: ...
12
by: Susan Cranford | last post by:
Please forgive, I have looked at so much info I can't figure out how to put it together even though I know it must be fairly simple. I have an...
20
by: Steve Jorgensen | last post by:
Hi all, I've just finished almost all of what has turned out to be a real bear of a project. It has to import data from a monthly spreadsheet...
6
by: Alpha | last post by:
I retrieve a table with only 2 columns. One is a auto-generated primary key column and the 2nd is a string. When I add a new row to the dataset to...
2
by: Leszek | last post by:
Hello! Can someone help me? How to get for some records (like 0 or 1) column names? Example: I've got a table, that looks server |...
2
by: MattB | last post by:
I know I can use dataviews and more looping to do this, but I'm wondering if there's a more elegant or more concise way to do this. I've got two...
0
by: Manish | last post by:
Hey Guys I am using a datagrid to extract information out of SQL Server datbase. The fields extracted are category,week,budget,Last Year,Forecast...
6
by: vj | last post by:
I have a program which generates xml files for excel but these files are not recognized by open office calc. I looked at the OO uno library, but it...
7
by: sparks | last post by:
I am working on a database that has a lot of calculated values on the forms. These were never put into the tables. But were tied to unbound fields...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

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.