Click here to Skip to main content
15,611,970 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
table: rawmaterialexpenses

coloumn: rawmaterialcode,expense,id(primary key)

query: how to get expense where rawmaterialcode = 1 and id is maximum

Try this:
SELECT MAX(id), expense FROM rawmaterialexpenses WHERE rawmaterialcode = 1 GROUP BY ID, expense
Share this answer
Prasad_Kulkarni 20-Apr-12 5:34am    
Thank You
Karthik Harve 20-Apr-12 5:31am    
@Prasad : your query does not work. You have to use group by clause.
Prasad_Kulkarni 20-Apr-12 5:41am    
Oops! I forget. Please see updated answer.
Karthik Harve 20-Apr-12 5:32am    
Question is to select the expense where rawmaterialcode = 1 and id is maximum
pandya purvang 20-Apr-12 5:33am    
ur query is wrong.
but i got the right one that is : "select expense from rawmaterialexpanses where id=(select max(id) from rawmaterialexpanses where rawmaterialcode=1) "
btw thanks.....
Try this query. you can use sub query
SELECT expense FROM rawmaterialexpenses WHERE rawmaterialcode = 1
AND id = (SELECT MAX(id) FROM rawmaterialexpenses)
Share this answer
P.Salini 20-Apr-12 5:37am    
my 5!
select expense from rawmaterialexpenses  where id =(select max(id) from rawmaterialexpenses ) AND rawmaterialcode = 1
Share this answer
Karthik Harve 20-Apr-12 5:25am    
@Gupta : "MAX" function will return only one value. So no meaning of using "IN" operator on that. Check your answer.
Arav Pradeep Gupta 20-Apr-12 5:45am    
Sure I revised the solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900