---
title: bitmap_construct_agg aggregate function
description: Learn the syntax of the bitmap\_construct\_agg function of the SQL language in Databricks SQL and Databricks Runtime.
canonical_url: https://docs.databricks.com/aws/en/sql/language-manual/functions/bitmap_construct_agg
---

# `bitmap_construct_agg` aggregate function

**Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime 13.3 LTS and above

Returns the bitwise `OR` of all bit positions values between 0 and 32767 in a group as a `BINARY`.

The function is commonly used to count the number of a dense set of distinct integral numbers in combination with the [bitmap\_bucket\_number()](https://docs.databricks.com/aws/en/sql/language-manual/functions/bitmap_bucket_number.md), [bitmap\_count()](https://docs.databricks.com/aws/en/sql/language-manual/functions/bitmap_count.md), and [bitmap\_bit\_position()](https://docs.databricks.com/aws/en/sql/language-manual/functions/bitmap_bit_position.md) functions.

To aggregate bitmaps in form of integral numerics use the [bit\_or()](https://docs.databricks.com/aws/en/sql/language-manual/functions/bit_or.md) aggregate function.

## Syntax

```
bitmap_construct_agg(expr) [FILTER ( WHERE cond ) ]
```

This function can also be invoked as a [window function](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-window-functions.md) using the `OVER` clause.

## Arguments

* `expr`: An integral numeric expression between 0 and 32767 representing a bit position in a `BINARY` string.
* `cond`: An optional boolean expression filtering the rows used for aggregation.

## Returns

A `BINARY` of length 4096.

## Examples

```sql
> SELECT hex(trim(TRAILING X'00' FROM bitmap_construct_agg(val))) FROM VALUES(0) AS T(val);
 01

> SELECT hex(trim(TRAILING X'00' FROM bitmap_construct_agg(val))) FROM VALUES(15) AS T(val);
 0080

> SELECT hex(trim(TRAILING X'00' FROM bitmap_construct_agg(val)))
   FROM VALUES(1), (3), (7), (15), (31) AS T(val);
 8A800080

-- Count the number of distinct values
> SELECT sum(num_distinct) AS num_distinct
    FROM (SELECT bitmap_bucket_number(val),
                 bitmap_count(bitmap_construct_agg(bitmap_bit_position(val)))
            FROM VALUES(1), (2), (1), (-1), (5), (0), (5) AS t(val)
            GROUP BY ALL) AS distinct_vals_by_bucket(bucket, num_distinct)
  5
```

## Related functions

* [`bit_or` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/bit_or.md)
* [`bitmap_and_agg` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/bitmap_and_agg.md)
* [`bitmap_bit_position` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/bitmap_bit_position.md)
* [`bitmap_bucket_number` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/bitmap_bucket_number.md)
* [`bitmap_count` function](https://docs.databricks.com/aws/en/sql/language-manual/functions/bitmap_count.md)
* [`bitmap_or_agg` aggregate function](https://docs.databricks.com/aws/en/sql/language-manual/functions/bitmap_or_agg.md)
