Skip to content

Instantly share code, notes, and snippets.

@r38y
Created April 14, 2014 22:03
Show Gist options
  • Save r38y/10685915 to your computer and use it in GitHub Desktop.
Save r38y/10685915 to your computer and use it in GitHub Desktop.
-- winning_count is a random number between 0 and 1
-- times the sum of the weights
SELECT id
FROM (
SELECT id, SUM(weight) OVER (ORDER BY id) S
FROM entries WHERE drawing_id = #{@drawing.id}
) Q
WHERE S >= winning_count
ORDER BY id
LIMIT 1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment