473,473 Members | 1,988 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 4166
* 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 array of input text boxes (txtDOBn) where n is...
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 export from another program, and convert that into...
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 be updated back to the database. What should I...
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 | parameter1 | parameter2 | parameter3...
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 DataTables with a column called guest_no. I'd love...
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 and Projection. Also i add a calculated column...
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 seems like an over kill. In my experience, for...
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 on the forms. Now 8000 records later they want...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.