Forum OpenACS Q&A: Response to Need some help on a query!

Collapse
Posted by Jonathan Ellis on
It doesn't need to be that complicated.

The reason you don't need distinct is that you can join the tables together such that only one row is returned per match, which is what I was trying to get at. I missed the fact that you will need two subqueries, since you need the latest game w/in a match AND the latest move w/in a game.

Here is what I mean. My tables' pkeys are all called id instead of table_id but the sense should be clear:
select ma.id as match, mo.id as move, ga.id as game, mo.latest_move
from matches ma, moves mo, games ga
where ga.match_id = ma.id
  and ga.id = (select max(id) from games ga2 where ga2.match_id = ma.id)
  and mo.game_id = ga.id
  and mo.id = (select max(id) from moves mo2 where mo2.game_id = ga.id)