Skip to content

Instantly share code, notes, and snippets.

@brhoades
Created December 4, 2014 02:04
Show Gist options
  • Save brhoades/d9f909c29b1461da7bb8 to your computer and use it in GitHub Desktop.
Save brhoades/d9f909c29b1461da7bb8 to your computer and use it in GitHub Desktop.
Returns a probability distribution of words
CREATE OR REPLACE FUNCTION somefuncname() RETURNS int LANGUAGE plpgsql AS $$
DECLARE
tmp int;
BEGIN
CREATE TEMP TABLE res AS SELECT id, SUM(count) OVER (ORDER BY id DESC) AS cum_sum FROM chains WHERE wid=## GROUP BY id ORDER BY cum_sum DESC;
tmp := FLOOR(random()*( SELECT max(cum_sum) FROM res LIMIT 1 ));
RETURN (SELECT cum_sum FROM res WHERE cum_sum > tmp ORDER BY cum_sum ASC LIMIT 1 );
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment