I need to get three fields from a table. The table has the fields – id, timestamp, viewTime. I need to get the min(timestamp), max(timestamp), viewTime of the record with max(timestamp). I would rather have only one query.
SELECT MAX(timestamp),
MIN(timestamp),
viewTime
FROM session_progress
WHERE session_id = 2374;
This query return the min, max correctly, but returns the minimum viewTime. WHat I really want is the viewTime of the record that has the maximum timestamp.
Ex:
timestamp viewTime
----------------------------------
2011-11-05 10:21:00 1055
2011-11-06 15:00:00 8900
2011-11-07 18:20:00 750
2011-11-07 19:23:00 4200
The query returns viewTime 750, but I need the viewTime of the max, which is 4200.
If the answer is written in grails, even better (this is same as the SQL query above):
def sp_res = sp.get {
projections {
min("timestamp")
max("timestamp")
totalTime
}
and {
eq("sessionId", unSession.id)
}
}
Thats just as idea, I have not tested it though. Pardon any bugs.