Skip to content

Instantly share code, notes, and snippets.

@pstef
Created July 27, 2020 16:07
Show Gist options
  • Save pstef/403a382771a6e47e04659113ba4ca389 to your computer and use it in GitHub Desktop.
Save pstef/403a382771a6e47e04659113ba4ca389 to your computer and use it in GitHub Desktop.
INSERT INTO x_y
SELECT l.x_id, unnest(array_positions) - 1
FROM (
SELECT x_y.x_id, bit_or(1 << y.id) AS yids
FROM x_y
GROUP BY x_y.x_id
HAVING count(*) <> (SELECT count(*) FROM y)
) AS l,
reverse(((SELECT bit_or(1 << y.id) FROM y) # l.yids)::bit(64)::text),
string_to_array(reverse, ''),
array_positions(string_to_array, '1');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment