Solved

Highest number from multiple numbers in a column

Posted on 2006-10-28
14
522 Views
Last Modified: 2008-02-01
If you have multiple rows for and item, how can you select the highest number in a column.

Example:

Item    Supplier    cost
100     shopper     3.50
100     Freds        3.60
100      Sams       3.55


Answer I need would be 3.60
0
Comment
Question by:DGinnett
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
  • +1
14 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17826557
select item, max(cost) from yourtable
group by item
0
 
LVL 9

Expert Comment

by:riaancornelius
ID: 17826566
What database is it? MySQL, MSSQL, Access... ?

for MSSQL:
select TOP 1 cost from table1 ORDER BY cost
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17826779
small correction to riaancornelius, as the default order by is ASC:


select TOP 1 cost from table1 ORDER BY cost DESC

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:riaancornelius
ID: 17826845
of course. Thanks angel :)
0
 
LVL 1

Expert Comment

by:Aristo
ID: 17827057
SELECT MAX(cost)
FROM TableName

This will give you one result for all the table:
3.60

If you need a list of the highest price for each product then:
SELECT item,MAX(cost)
FROM TableName
GROUP BY item

This will give you one result for each item:
item                cost
-----               ------
100                 3.60
101                 10.1
  .                     .
  .                     .
  .                     .
0
 

Author Comment

by:DGinnett
ID: 17827606
DB is Access
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17828062
did you already try the suggestions with MAX() and GROUP BY ?
should work fine in access...
0
 

Author Comment

by:DGinnett
ID: 17829032
I'm using this in query and in the expression builder I have

item_LC: Format(
(select  [Item]![Item_style] , max( [Supplier_data]![LC_cost])
from [Supplier_data]
group by [Supplier_data]![LC_cost])
,"#.00")

When I run the query I get

you have written a subquery that can return more than one field without using the exist reserved word in the main query's from clause.  revise the select statement of the subquery to request only one field.

Also, how can you store this info in a table fieldname
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17829077
first, you used GROUP BY LC_Cost field, but actually seems you want to max of that. that is not correct.

now, you did not answer the question:
do you want to overall global max value, or per item_style?

global overall max value (highest cost):
item_LC: Format( DMax( "LC_Cost", "Supplier_data", "") ,"#.00")

per current style (column item_style assumed in the existing query:
item_LC: Format( DMax( "LC_Cost", "Supplier_data", "Item_Style='"& Item_Style & "' " ) ,"#.00")
0
 

Author Comment

by:DGinnett
ID: 17829080
Addition to above,  Added supplier_data table and linked style_no.

ran again and getting....

Now getting SQL statement could not be executed because it contins ambiguous outer joins.  To force one of the joins to be performed first, create a separate query that performs the first join and then inlude the query in your SQL statement.
0
 

Author Comment

by:DGinnett
ID: 17829087
angelIII per your response 10/29/2006 08:03AM PST

max value per item_style.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17829185
per current style (column item_style assumed in the existing query:
item_LC: Format( DMax( "LC_Cost", "Supplier_data", "Item_Style='"& Item_Style & "' " ) ,"#.00")
0
 

Author Comment

by:DGinnett
ID: 17829188
angelIII

I did get it to work in the following statement

item_LC: Format(DMax("LC_Cost","Supplier_data","[Style_no]='" & [Item]![Item_Style] & "' "),"#.00")
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 125 total points
ID: 17829200
glad I could help
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question