Solved

Highest number from multiple numbers in a column

Posted on 2006-10-28
14
478 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
  • 6
  • 5
  • 2
  • +1
14 Comments
 
LVL 142

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 142

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
 
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 142

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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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 142

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 142

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 142

Accepted Solution

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I guess that all of us know that caching the data usually increase the performance, but I worried if all of us are aware about the risk that caching the data provides and how to minimize this.  That’s the reason why I decided to write this short art…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

914 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now