Now that postgres supports windowing functions.
SELECT group_id, title, checkout_date) FROM
(SELECT group_id,
title,
checkout_date,
rank() OVER (PARTITION BY group_id ORDER BY checkout_date) AS rk
) AS subq
WHERE rk=1;
You probably need an index on (group_id, checkout_date), perhaps the other way around. I did not hit the window to find out what the planner was trying to do.
source
share