Skip to content

Instantly share code, notes, and snippets.

@sheatsb
Created May 28, 2020 22:35
Show Gist options
  • Select an option

  • Save sheatsb/37a87ef41fce8e5033d05ffa0f5bbfdf to your computer and use it in GitHub Desktop.

Select an option

Save sheatsb/37a87ef41fce8e5033d05ffa0f5bbfdf to your computer and use it in GitHub Desktop.
random rows from postgres
CREATE
OR REPLACE FUNCTION public.random_recipient() RETURNS SETOF recipients LANGUAGE sql STABLE AS $ function $
SELECT
*
FROM
recipients OFFSET floor(
random() * (
SELECT
COUNT(*)
FROM
recipients
WHERE
is_active = true
)
)
LIMIT
1;$ function $
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment