Click here to Skip to main content
16,017,238 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hello,

I try to select the MAX date from a table who has a specific value, something like that:

SQL
SELECT regRaport.regRaport_id, regRaport.regRaport_date, regRaport.regRaport_soldinitial, regRaport.regRaport_soldfinal, IIF(regRaport.regRaport_categorie Is Null, 0, regRaport.regRaport_categorie) AS RegRaportCategorie
FROM regRaport
WHERE regRaport.regRaport_date=(select MAX(regRaport.regRaport_date) from regRaport  WHERE regRaport_categorie=5);


but the above query returns all value for MAX() date, not for a specific category.
Any suggestions?
Posted
Updated 6-Feb-14 22:00pm
v2
Comments
Sampath Lokuge 7-Feb-14 4:04am    
Check this: http://stackoverflow.com/questions/7118170/sql-server-select-only-the-rows-with-maxdate
Sampath Lokuge 7-Feb-14 4:05am    
Another one.http://stackoverflow.com/questions/12942306/sql-select-maxdate-and-corresponding-value

1 solution

Try this

SQL
SELECT	regRaport.regRaport_id,
	regRaport.regRaport_date,
	regRaport.regRaport_soldinitial,
	regRaport.regRaport_soldfinal,
	IIF(regRaport.regRaport_categorie Is Null, 0, regRaport.regRaport_categorie) AS RegRaportCategorie
FROM	regRaport
inner join (select regRaport.regRaport_id as ID,
				MAX(regRaport.regRaport_date) as regDate
		from	regRaport
		WHERE	regRaport_categorie = 5) reg
	on  regRaport.regRaport_id = reg.ID
	and regRaport.regRaport_date = reg.regDate
 
Share this answer
 
v2

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