1

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?

asked Jun 26, 2015 at 1:20

1 Answer 1

3

bit_or is an aggregate function, like sum or count.

SELECT bit_or(flags) FROM items;
answered Jun 26, 2015 at 1:59

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.