473,406 Members | 2,293 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,406 software developers and data experts.

How to select other data columns when using max function?

167 100+
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 miles so I am using the max function... Select max(miles) from table. It works and returns the largest number in this column. I also need the state that is associated with the max miles so I changed my select to:
Select state, max(miles) from table. However, when I run this select I get an error saying that 'state' is not part of an aggregate function. If I use any aggregate function on 'state', I do not get the correct info. How do I get other data columns from the same row the max function returns???
Oct 16 '07 #1
3 2697
Rabbit
12,516 Expert Mod 8TB
You have to tell it to group by state.
Expand|Select|Wrap|Line Numbers
  1. SELECT State, Max(Miles) AS MaxOfMiles
  2. FROM SomeTable
  3. GROUP BY State;
  4.  
Oct 16 '07 #2
ncsthbell
167 100+
This did not give me the results I needed. My data, for example has multiple rows for each of the states. Example:
St Miles
AL 100
AL 200
AL 300
WV 250
WV 350
WV 450
When I put the group by state on the select statement, it returned a row for each state with the max miles for that state so what I got was AL-300 miles, WV-450 miles, and etc. What I need is the max miles over all the rows of data, regardless of the state and whatever row it selects for the max miles, I need to know the state. Does this make sense?
Oct 17 '07 #3
Rabbit
12,516 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. SELECT TOP 1 State, Miles
  2. FROM SomeTable
  3. ORDER BY Miles DESC;
  4.  
This will return all records that tie for highest miles though.
Oct 17 '07 #4

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

Similar topics

5
by: | last post by:
Is it possible to extend this function to dynamically create a combo boxes from the db table....How would do this...I tried passing a 'Select' type to the function but got a blank entry....Any...
3
by: Hendry Taylor | last post by:
I have a problem where if I issue a select * from against a database it returns no data, but if I select column from it returns the data. Why would the * not be working as a wildcard?
4
by: Bruce Pullum | last post by:
I have a datagrid that I am using a DataView with. All works great for the sorting of the columns. However, after I sort the column, and then try and select a data row to edit, the row selected...
3
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I...
7
by: localhost | last post by:
A DataGrid with shows a label in one of the columns when in view mode. When in edit mode, I want to show a dropdown, and have the default selection set to what the textbox used to be. Right now...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
3
by: shripaldalal | last post by:
hi all, i have the following fields.... Document_Name, Document_No, Document_Date, Order_No, Order_Date, Peripheral The first 5 you can understand, peripheral is a memo field which...
22
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
1
by: Sunray | last post by:
I have a form called the sales form and i have 2 sets of listboxes So what happens is. i add items form the bottom set of list boxes which are bound to a data base to the top set of list boxes which...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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...

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.