user1511031
user1511031

Reputation: 13

mysql select and MAX()

I am going nuts already, don't know what else to try. I got this data:

Table name is erste

valuta  kupovni    
EUR     7.435   
GBP     9.2132  

And then I try this:

SELECT valuta, MAX(kupovni)
FROM erste

I get this:

valuta  MAX(kupovni)
 EUR    9.213199615478516

And I want to get:

GBP 9.2132

Column valuta is varchar(3) and kupovni is float (10). I really dont know what I am doing it wrong. When I try:

SELECT valuta, MAX(kupovni)
FROM erste
GROUP BY kupovni

It does it right?

Upvotes: 1

Views: 1823

Answers (4)

Sashi Kant
Sashi Kant

Reputation: 13465

Try this:

SELECT valuta, TRUNCATE(MAX(kupovni),4)
FROM erste
group by valuta

Upvotes: 1

sel
sel

Reputation: 4957

SELECT erste.* 
  FROM ( SELECT valuta, MAX(kupovni) AS maxtotal
           FROM erste
           GROUP BY valuta) AS dt
INNER JOIN erste
    ON erste.valuta= dt.valuta
   AND erste.kupovni= dt.maxtotal 
ORDER BY items_purchased.val DESC LIMIT 1 

Upvotes: 0

Zane Bien
Zane Bien

Reputation: 23135

This merely seems like a simple matter of selecting the row with the highest value for the kupovni field.

You can do:

SELECT valuta, kupovni
FROM erste
ORDER BY kupovni DESC
LIMIT 1

Upvotes: 4

Bohemian
Bohemian

Reputation: 425318

You are very close! Here's what you need:

SELECT valuta, MAX(kupovni)
FROM erste
where valuta = 'GBP' -- optional where clause
group by valuta

You must "group by" all columns not aggregated by an aggregate function.

Unless you really need the valuta in the output, because you know valuta, I would do this:

SELECT MAX(kupovni)
FROM erste
where valuta = 'GBP'

(Note that mysql allows columns to be omitted from the group by, in which case you'll get the only the first row encountered for each group)

Upvotes: 1

Related Questions