473,835 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
+ 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 4182
* 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******@sympa tico.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******** ***********@new ssvr12.news.pro digy.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(aL ist))
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******** **********@news svr12.news.prod igy.com...
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(aL ist))
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
4723
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: +--------------+-----------------+-----+----+--------+--------------+ | Field | Type | Null| Key| Default|Extra | +--------------+-----------------+-----+----+--------+--------------+ | id | int(10) unsigned| | PRI| NULL |auto_increment| | timestamp | datetime ...
12
7019
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 created at load. On the onchange event I want to calc the age and show in adjacent input text boxes that are readonly and also arrays (an age calced for each DOB entered). I was going to use the datediff function in vbscript to do the calc. Can...
20
2777
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 normalized data. The task is made more difficult by the fact that the structure itself can vary from month to month (in well defined ways). So, I used the SQL-centric approach, taking vertical stripes at a time so that, for instance, for each...
6
5075
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 do with the 1st column ? (Below I have a "1" in place for now). Also, Does the datase.AcceptChanges(); updates the changes to the database? Which command do I use to update the changes in dataset back to the Access database table? Thanks, Alpha...
2
1619
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
7970
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 to be able to use something like the .IndexOf method of arrays and bound controls. Somehting like: Dim r as DataRow For Each r in DataTable2.Rows If DataTable1.Columns("guest_no").IndexOf(r("guest_no")) <> -1
0
1797
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 Week% to the datatable. Also i add calculated row - SPLH(Revenue/Hours) and AWR(Payroll/Hours) for each week. My grid layout is as follows- Catgory Week Budget LY Fcst Projection Week% Revenue 1 2000 1500 2400 2000 Cost ...
6
2688
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 simpler documents, it is much faster to directly write to underlying XML format. Has anyone done this? Any idea on the XML format used by Open Office Calc? VJ
7
1778
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 these values in the tables. All of this is done in modules after update of certain values entered on the forms. The calculations are called on current of the forms so they are recalculated each time the form is opened. I have no idea why it was...
0
10812
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10235
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9346
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7766
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5638
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5804
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4434
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 we have to send another system
2
3995
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3089
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.