Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

bit_or is an aggregate function, like sum or count.

SELECT bit_or(flags) FROM items;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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