mardi 9 novembre 2021

Select random rows from Postgres table weighted towards value in column

Users are presented with 2 random items from an assets table to vote on.

There is a votes_count column in the assets table that counts each time users vote.

When choosing the 2 random items, I'd like to weight that more towards a lower value in votes_count. So items with a lower vote count have a higher probability of being selected randomly.

How can I do this with Postgres?

I've used various methods for selecting random records (RAND(), TABLESAMPLE BERNOULLI(), TABLESAMPLE SYSTEM()), but those don't have the weighting that I'm after.

I'm running PostgreSQL 13, FWIW.




Aucun commentaire:

Enregistrer un commentaire