// aggregates reference

SQL BIT_OR function

ORs the values of a group together bit by bit.

类别
Aggregates
Returns
integer
In this category
42 of 52

语法

BIT_OR(x) | BIT_OR_AGG(x)

引擎支持

引擎Support备注
SQL Server Not built in No built-in equivalent.
PostgreSQL 是 aggregate over integer and bit types; empty group returns NULL
MySQL 是 empty or all-NULL group returns 0
SQLite Not built in No built-in equivalent.
Oracle Different name or syntax 21c+: BIT_OR_AGG; empty or all-NULL group returns 0

示例

MySQL

SELECT user_id, BIT_OR(perm_bits) AS all_granted FROM grants GROUP BY user_id;

Oracle

SELECT user_id, BIT_OR_AGG(perm_bits) FROM grants GROUP BY user_id;

值得了解

  • The OR identity is 0, so MySQL and Oracle return 0 for an empty group; PostgreSQL returns NULL.
  • NULLs are skipped.
  • Use it to collect every flag seen in a group: any row setting a bit keeps it set in the result.

Questions about BIT_OR

What does an empty group return?

0 in MySQL and Oracle, the OR identity; NULL in PostgreSQL, following the usual aggregate rule.

BIT_OR vs MAX for flags?

They agree when each row holds one flag value of 0 or 1. With masks they differ: MAX(4, 3) is 4, BIT_OR(4, 3) is 7.

Does PostgreSQL have an aggregate BIT_OR?

Yes, bit_or(x) is a built-in aggregate over integer and bit types.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: PostgreSQL, MySQL, Oracle.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.