Solved

Highest number from multiple numbers in a column

Posted on 2006-10-28
14
501 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 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
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.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
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…

856 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