14

I have a table that is roughly:

id | category | link | caption | image

My goal is to fetch a random row from each distinct category in the table, for all the categories in the table. The plan is to then assign each row to a variable for its respective category.

Right now I'm using multiple SELECT statements resembling:

SELECT link, caption, image FROM table
       WHERE category='whatever'
       ORDER BY RANDOM() LIMIT 1`

But this seems inelegant and creates more trips to the DB, which is expensive.

I'm pretty sure there's a way to do this with window functions in Postgres, but I have no experience with them and I'm not entirely sure how to use one to get what I want.

1 Answer 1

29

Try something like:

SELECT DISTINCT ON (category) *
FROM table 
ORDER BY category, random();

Or with window functions:

SELECT * 
FROM (
SELECT *, row_number() OVER (PARTITION BY category ORDER BY random()) as rn
FROM table ) sub
WHERE rn = 1;
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent! Both of those seem to work for my purposes. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.