tags:

views:

23

answers:

2

ID NAME AMT
1 Name1 1000
2 Name2 500
3 Name3 3000
4 Name1 5000
5 Name2 2000
6 Name1 3000

consider above table as sample.

am having a problem in my sql query, Am using like this.

Select name,amt from sample where amt between 1000 and 5000

it returns all the values in the table between 1000 and 5000, instead I want to get maximum amount record for each name

i.e.,
3 name3 3000
4 name1 5000
5 name2 2000

A: 
SELECT 
    name,MAX(amt) AS amt
FROM 
    table 
WHERE 
    amt BETWEEN 1000 and 5000 
GROUP BY name

Here you're grouping by the name column, and retrieving the maximum value of the column 'amt' within that GROUP, for all of the rows where amt is between 1000 and 5000. This query is untested, but is simple enough - I believe it should work.

Seidr
its not working
deep
Fair enough, looks like Coronatus got it. My bad
Seidr
hey thanks seidr, your query working... my innodb issue.. its working
deep
Hah cool, it's all good :)
Seidr
hai i want to get id field also when i add id into that query, it shows the id wrongly... can u plz update id field into the query ???
deep
+1  A: 
Select name,amt
from sample
where amt between 1000 and 5000
ORDER BY amt DESC
GROUP BY name
Coronatus
i want to get id field also..
deep
I would use this query then deep - otherwise as described my query returns the wrong ID.Simply add id to the list of columns returned
Seidr