Available in versions: Dev (3.18) | Latest (3.17) | 3.16

BIT_OR_AGG

Applies to ✅ Open Source Edition   ✅ Express Edition   ✅ Professional Edition   ✅ Enterprise Edition

An aggregate function to perform the equivalent of the BIT_OR function on a data set. In other words, the resulting bits are:

  • 1 at position p if the argument is 1 at position p for at least one row in the group.
  • 0 at position p if the argument is 0 at position p for every row in the group.

As with most aggregate functions, NULL values are not aggregated.

SELECT 
  bit_or_agg(ID),
  bit_or_agg(AUTHOR_ID)
FROM BOOK
create.select(
         bitOrAgg(BOOK.ID),
         bitOrAgg(BOOK.AUTHOR_ID))
      .from(BOOK)

Producing:

+------------+------------+
| bit_or_agg | bit_or_agg |
+------------+------------+
|          7 |          3 |  
+------------+------------+

Dialect support

This example using jOOQ:

bitOrAgg(BOOK.ID.coerce(TINYINT))

Translates to the following dialect specific expressions:

-- ASE, MEMSQL, REDSHIFT, SQLDATAWAREHOUSE, SQLITE, SQLSERVER
(CASE max(CASE (BOOK.ID & 1)
  WHEN 0 THEN 0
  WHEN 1 THEN 1
END)
  WHEN 1 THEN 1
  WHEN 0 THEN 0
END + CASE max(CASE (BOOK.ID & 2)
  WHEN 0 THEN 0
  WHEN 2 THEN 2
END)
  WHEN 2 THEN 2
  WHEN 0 THEN 0
END + CASE max(CASE (BOOK.ID & 4)
  WHEN 0 THEN 0
  WHEN 4 THEN 4
END)
  WHEN 4 THEN 4
  WHEN 0 THEN 0
END + CASE max(CASE (BOOK.ID & 8)
  WHEN 0 THEN 0
  WHEN 8 THEN 8
END)
  WHEN 8 THEN 8
  WHEN 0 THEN 0
END + CASE max(CASE (BOOK.ID & 16)
  WHEN 0 THEN 0
  WHEN 16 THEN 16
END)
  WHEN 16 THEN 16
  WHEN 0 THEN 0
END + CASE max(CASE (BOOK.ID & 32)
  WHEN 0 THEN 0
  WHEN 32 THEN 32
END)
  WHEN 32 THEN 32
  WHEN 0 THEN 0
END + CASE max(CASE (BOOK.ID & 64)
  WHEN 0 THEN 0
  WHEN 64 THEN 64
END)
  WHEN 64 THEN 64
  WHEN 0 THEN 0
END + CASE max(CASE (BOOK.ID & -128)
  WHEN 0 THEN 0
  WHEN -128 THEN -128
END)
  WHEN -128 THEN -128
  WHEN 0 THEN 0
END)

-- AURORA_MYSQL, AURORA_POSTGRES, H2, ORACLE, SNOWFLAKE
bit_or_agg(BOOK.ID)

-- BIGQUERY, COCKROACHDB, MARIADB, MYSQL, POSTGRES, SYBASE, VERTICA, YUGABYTEDB
bit_or(BOOK.ID)

-- DB2, HANA, HSQLDB, INFORMIX, TERADATA
(CASE max(CASE bitand(
  BOOK.ID,
  1
)
  WHEN 0 THEN 0
  WHEN 1 THEN 1
END)
  WHEN 1 THEN 1
  WHEN 0 THEN 0
END + CASE max(CASE bitand(
  BOOK.ID,
  2
)
  WHEN 0 THEN 0
  WHEN 2 THEN 2
END)
  WHEN 2 THEN 2
  WHEN 0 THEN 0
END + CASE max(CASE bitand(
  BOOK.ID,
  4
)
  WHEN 0 THEN 0
  WHEN 4 THEN 4
END)
  WHEN 4 THEN 4
  WHEN 0 THEN 0
END + CASE max(CASE bitand(
  BOOK.ID,
  8
)
  WHEN 0 THEN 0
  WHEN 8 THEN 8
END)
  WHEN 8 THEN 8
  WHEN 0 THEN 0
END + CASE max(CASE bitand(
  BOOK.ID,
  16
)
  WHEN 0 THEN 0
  WHEN 16 THEN 16
END)
  WHEN 16 THEN 16
  WHEN 0 THEN 0
END + CASE max(CASE bitand(
  BOOK.ID,
  32
)
  WHEN 0 THEN 0
  WHEN 32 THEN 32
END)
  WHEN 32 THEN 32
  WHEN 0 THEN 0
END + CASE max(CASE bitand(
  BOOK.ID,
  64
)
  WHEN 0 THEN 0
  WHEN 64 THEN 64
END)
  WHEN 64 THEN 64
  WHEN 0 THEN 0
END + CASE max(CASE bitand(
  BOOK.ID,
  -128
)
  WHEN 0 THEN 0
  WHEN -128 THEN -128
END)
  WHEN -128 THEN -128
  WHEN 0 THEN 0
END)

-- FIREBIRD
(CASE max(CASE bin_and(
  BOOK.ID,
  1
)
  WHEN 0 THEN 0
  WHEN 1 THEN 1
END)
  WHEN 1 THEN 1
  WHEN 0 THEN 0
END + CASE max(CASE bin_and(
  BOOK.ID,
  2
)
  WHEN 0 THEN 0
  WHEN 2 THEN 2
END)
  WHEN 2 THEN 2
  WHEN 0 THEN 0
END + CASE max(CASE bin_and(
  BOOK.ID,
  4
)
  WHEN 0 THEN 0
  WHEN 4 THEN 4
END)
  WHEN 4 THEN 4
  WHEN 0 THEN 0
END + CASE max(CASE bin_and(
  BOOK.ID,
  8
)
  WHEN 0 THEN 0
  WHEN 8 THEN 8
END)
  WHEN 8 THEN 8
  WHEN 0 THEN 0
END + CASE max(CASE bin_and(
  BOOK.ID,
  16
)
  WHEN 0 THEN 0
  WHEN 16 THEN 16
END)
  WHEN 16 THEN 16
  WHEN 0 THEN 0
END + CASE max(CASE bin_and(
  BOOK.ID,
  32
)
  WHEN 0 THEN 0
  WHEN 32 THEN 32
END)
  WHEN 32 THEN 32
  WHEN 0 THEN 0
END + CASE max(CASE bin_and(
  BOOK.ID,
  64
)
  WHEN 0 THEN 0
  WHEN 64 THEN 64
END)
  WHEN 64 THEN 64
  WHEN 0 THEN 0
END + CASE max(CASE bin_and(
  BOOK.ID,
  -128
)
  WHEN 0 THEN 0
  WHEN -128 THEN -128
END)
  WHEN -128 THEN -128
  WHEN 0 THEN 0
END)

-- ACCESS, DERBY, EXASOL
/* UNSUPPORTED */

(These are currently generated with jOOQ 3.18, see #10141), or translate your own on our website

Feedback

Do you have any feedback about this page? We'd love to hear it!

The jOOQ Logo