I wish to use PostgreSQL's bit_or function on several rows of a single column of my table, but I can't figure out the proper usage for this.

Suppose I have bitwise flags in my column flags, and the rows of interest hold the numbers 1 (B00000001), 2 (B00000010), and 13 (B00001101). I want the output to be 15 (B00001111).

What I've tried so far is as follows:

SELECT bit_or(SELECT flags FROM items);
-- ERROR:  syntax error at or near "select"
SELECT bit_or((SELECT flags FROM items));
-- ERROR:  more than one row returned by a subquery used as an expression
SELECT bit_or(array(SELECT flags FROM items));
-- ERROR:  function bool_or(integer[]) does not exist
SELECT bit_or(select array(SELECT flags FROM items));
-- ERROR:  syntax error at or near "select"

Do you have any advice?


bit_or is an aggregate function, like sum or count.

SELECT bit_or(flags) FROM items;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.