Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

We have query as below. What we need is to select particular training with latest date and only single data but now when we run the query below we get more then one results.

SELECT tblTraining.clientTrainingID AS lastID, MAX( tblTraining.trainingDate ) AS lastDate
FROM tblTraining
WHERE tblTraining.trainedID =37
GROUP BY tblTraining.clientTrainingID
share|improve this question
    
I have solved it. –  newbie14 Nov 5 '12 at 13:13
1  
Can you share the solution? Was it a problem in your data? –  JNK Nov 5 '12 at 14:04
    
No it was the problem with the group by I added wrong key there. –  newbie14 Nov 6 '12 at 14:17
add comment

closed as too localized by JNK Nov 6 '12 at 13:18

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

up vote 2 down vote accepted

Try

SELECT 
  tblTraining.clientTrainingID AS lastID,
  MAX( tblTraining.trainingDate ) AS lastDate
 FROM tblTraining
 WHERE tblTraining.trainedID =37
 GROUP BY tblTraining.clientTrainingID
 ORDER BY tblTraining.clientTrainingID desc
 LIMIT 1 

This will order the max() and limit the result to the first line being the highest value.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.