Skip to content

Instantly share code, notes, and snippets.

@abelsonlive
Created April 1, 2014 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abelsonlive/9924298 to your computer and use it in GitHub Desktop.
Save abelsonlive/9924298 to your computer and use it in GitHub Desktop.
generate a histogram of bin counts of a column given the min and max of the column and the number of bins.
CREATE OR REPLACE FUNCTION hist_sfunc (state INTEGER[], val REAL, min REAL, max REAL, nbuckets INTEGER) RETURNS INTEGER[] AS $$
DECLARE
bucket INTEGER;
i INTEGER;
BEGIN
bucket := width_bucket(val, min, max, nbuckets - 1) - 1;
IF state[0] IS NULL THEN
FOR i IN SELECT * FROM generate_series(0, nbuckets - 1) LOOP
state[i] := 0;
END LOOP;
END IF;
state[bucket] = state[bucket] + 1;
RETURN state;
END;
$$ LANGUAGE plpgsql IMMUTABLE;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment