473,396 Members | 1,816 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,396 software developers and data experts.

MAX() Aggregate Function with Multiple Columns

ebs57
18
Can someone please enlighten me as to how I can select the maximum value from a table with multiple columns in the SELECT line?

What I mean is, if I use this code:
Expand|Select|Wrap|Line Numbers
  1. SELECT Max(Revision)
  2. FROM Documents;
...everything is fine, but if I use this code:
Expand|Select|Wrap|Line Numbers
  1. SELECT Max(Revision), Title
  2. FROM Documents;
...frogs and hail. The error message is "You tried to execute a query that does not include the specified expression 'Title' as part of an aggregate function." Any help appreciated.
Jun 14 '07 #1
4 14639
MMcCarthy
14,534 Expert Mod 8TB
This should work

Expand|Select|Wrap|Line Numbers
  1. SELECT Max(Revision), Title
  2. FROM Documents
  3. GROUP BY Title;
Jun 14 '07 #2
ebs57
18
Ms. McCarthy,

Would you please explain why GROUP BY will appease the angry MAX() command gods? I am trying to get a handle on SQL and VBA and -- although I certainly appreciate the fix you've offered -- I would definitely like to understand the reasoning behind IBM's or whomever's reasoning for requiring MAX() all the columns in the SELECT line.

My impression from the error message was the SQL statement would not evaluate because it somehow wanted to apply MAX() to all columns in the selected table. Apparently, GROUP BY takes care of this??? Please use small words in your explanation.

- Lumpy
Jun 15 '07 #3
MMcCarthy
14,534 Expert Mod 8TB
Hi Lumpy

Think of it as a Logical statement. This give the Maximum Revision per Title. Select Maximum Revision for each Title in table Documents.

Without the Group By it would be trying to return the max value on each record, in other words every record.

When using aggregates (Sum, Avg, Count, Max, etc.) in a query you must always group by any field not aggregated.

If for example you had included an Revised By field in your query, that would also have to be included in the Group By.

Expand|Select|Wrap|Line Numbers
  1. SELECT Max(Revision), Title, RevisedBy
  2. FROM Documents
  3. GROUP BY Title, RevisedBy;
The results this time would be a Max Revision per Title per RevisedBy. So If a Title was revised by two people it would give the max revision for each.

Mary
Jun 15 '07 #4
ebs57
18
Holy Witchetty Grub -- I think I gets it!

Your opening statements really clarified it for me: "Select the Maximum Revision for each Title in table Documents." So, the other side of GROUP BY is to exclude columns from calculations.

Tanks...
Jun 15 '07 #5

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

Similar topics

6
by: Steven An | last post by:
Howdy, I need to write an update query with multiple aggregate functions. Here is an example: UPDATE t SET t.a = ( select avg(f.q) from dbo.foo f where f.p = t.y ), t.b = ( select sum(f.q)...
6
by: Wire | last post by:
Hello I want to sum these records on month(datum) and device and get then get the min and max values and the associated low high date/times this does not work select month(datum) as...
2
by: Wilder | last post by:
I'm trying to update a field in one table with the minimum values of the field in another table. The two tables are linked via a common field. I want to populate a date field in one table with...
8
by: Bill | last post by:
Hello out there; This may be a challenge but I'm certain it's possible but I can't seem to figure out how. I have a table that has several date fields, e.g., Date1, Date2, Date3, Date4 ......
3
by: ncsthbell | last post by:
I am pulling my hair out on this! Seems like it should be easy, I just can not get it to work like I want. I know I am doing something wrong, so I hope someone can be so kind to guide me!!! I...
3
by: ncsthbell | last post by:
I am trying to run a query in access using the 'max' function. For example, I have many rows of data, one for each state and each state has a column for 'Miles'. I want to get the highest number of...
0
by: okonita | last post by:
Hi all, I need to create one or two indexes on date columns to support MAX and MIN aggregate functions on a very large table and to make the SQL perform/run faster. I remember a group discussion...
4
by: vincibleman | last post by:
Howdy all, Working my way into SQL from Access. Think I might have the hang of the basics, but would really appreciate a sanity check. The stored procedure listed below works, but I can't help...
6
by: Kelii | last post by:
Hello, So I have a form which shows all items available for sale, when it was last sold, where it was last sold, and whether it is active or inactive. I would like to be able to edit the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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,...

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.