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. 7 4180
* 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.
"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.
* 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.
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.
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.
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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 ...
|
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...
|
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...
|
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...
|
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
----------------------------------------------
| |
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
|
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 ...
|
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
|
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...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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...
|
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |