Reputation: 847
Running a query which should return all open questions along with the last update time they were updated.
The way this works is a question is inserted into the Question table, and updates / answers are inserted to qUpdate table with a foreign key of the original question to associate them.
This is the query I've written at the moment however it's not quite what I want:
SELECT MAX (qUpdate.UpdateTime) AS LastUpdateTime, Question.ID FROM Question
LEFT JOIN qUpdate ON qID = qUpdate.qID WHERE Status = 'open'GROUP BY Question.ID, Question.Status;
The result:
lastupdatetime id
2012-12-21 20:37:00;7
2012-12-21 20:37:00;10
2012-12-21 20:37:00;1000
2012-12-21 20:37:00;5
2012-12-21 20:37:00;2
2012-12-21 20:37:00;9
As you can see it returns the latest update for all of the questions as oppose to the latest update for each of the questions. I understand why but I'm not sure how to solve this issue.
Sorry I was a bit ambiguous in my explanation..
Basically:
At present it returns the last update time regardless of what the update time of a particular question is i.e. it finds the maximum last update time for all of them and returns it for all the questions that are open...
What I'd like it to do is list all of the open questions as well as the last update time for that particular ticket.
Im using Postgresql
Upvotes: 0
Views: 96
Reputation: 102743
Try this. You don't need to group by the status.
SELECT MAX (qUpdate.UpdateTime) AS LastUpdateTime, Question.ID FROM Question
LEFT JOIN qUpdate ON qID = qUpdate.qID WHERE Status = 'open' GROUP BY Question.ID;
Upvotes: 0
Reputation: 53319
Looks like this is is your problem:
LEFT JOIN qUpdate ON qID = qUpdate.qID
Should be this:
LEFT JOIN qUpdate ON qID = Question.ID
Upvotes: 1