Solved

Highest number from multiple numbers in a column

Posted on 2006-10-28
14
465 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Creating and Managing Databases with phpMyAdmin in cPanel.
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…

757 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

19 Experts available now in Live!

Get 1:1 Help Now