SELECT page_id, user_id, clicks
FROM (
SELECT page_id, user_id, rank(page_id) as rank, clicks FROM (
SELECT page_id, user_id, clicks FROM mytable
DISTRIBUTE BY page_id
SORT BY page_id, clicks desc
) a ) b
WHERE rank < 5
ORDER BY page_id, rank
FROM (
SELECT page_id, user_id, rank(page_id) as rank, clicks FROM (
SELECT page_id, user_id, clicks FROM mytable
DISTRIBUTE BY page_id
SORT BY page_id, clicks desc
) a ) b
WHERE rank < 5
ORDER BY page_id, rank