Skip to content

Instantly share code, notes, and snippets.

@chrisvaughn
Created May 22, 2018 14:55
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 chrisvaughn/43d66b930b794920d8aa5e080ef5834f to your computer and use it in GitHub Desktop.
Save chrisvaughn/43d66b930b794920d8aa5e080ef5834f to your computer and use it in GitHub Desktop.
CREATE OR REPLACE FUNCTION current_streak_for_month(x integer, as_of date) RETURNS INTEGER AS
$$
DECLARE
shifted_checkin bigint;
rightmost_zero bigint;
BEGIN
shifted_checkin := x >> 31 - day_of_month(as_of);
rightmost_zero := ~shifted_checkin & (shifted_checkin + 1);
RETURN log(2, rightmost_zero)::INTEGER;
END
$$ LANGUAGE 'plpgsql';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment