> ## Documentation Index
> Fetch the complete documentation index at: https://docs.e6data.com/llms.txt
> Use this file to discover all available pages before exploring further.

# GROUPING

> Returns a number whose bits identify which GROUP BY expressions were omitted from the current grouping set.

<div className="e6-meta"><span className="e6-eyebrow e6-eyebrow--quiet">Aggregate function</span></div>

**Aliases:** [`grouping_id`](/query/sql/functions/aggregate/grouping_id)

A NULL data value in a grouping expression still counts as present and contributes zero. A one bit marks an expression omitted by the grouping set, independently of its data value.

## Syntax

### Aggregate

```sql theme={"theme":{"light":"github-light","dark":"github-dark"}}
grouping(expression[, additional_expression, ...])
```

* Use in an aggregate query, with one through 31 arguments present in its GROUP BY.

### Grouping id form (aggregate)

```sql theme={"theme":{"light":"github-light","dark":"github-dark"}}
grouping_id(expression[, additional_expression, ...])
```

* Use in an aggregate query, with one through 31 arguments present in its GROUP BY.

## Arguments

### Aggregate, grouping id form

| Name | Type | Required | Description |
| - | - | - | - |
| expression | GROUP BY expression | Yes | Expression whose presence in the current grouping set is tested. Use a column listed in the enclosing GROUP BY. At most 31 total arguments. |
| additional\_expression | GROUP BY expression | No | Expression whose presence in the current grouping set is tested. Use a column listed in the enclosing GROUP BY. At most 31 total arguments. This argument can repeat. |

## Returns

* Type: BIGINT or INTEGER, depending on the query; query tools report BIGINT
* Possible values: An integer bitmask. Each argument contributes 0 when present in the grouping set and 1 when omitted; the rightmost argument is the least significant bit. Ordinary GROUP BY returns zero.
* NULL behavior: A NULL data value in a grouping expression still counts as present and contributes zero. A one bit marks an expression omitted by the grouping set, independently of its data value.
* Empty input: The flag is computed for every group produced by the enclosing aggregate, including a total grouping set if that aggregate produces one. It does not itself create a row on empty input.

## Properties

| Property | Value |
| - | - |
| Deterministic | Yes |
| Supports DISTINCT | No |
| Supports window usage | No |
| Exact result | Yes |

## Usage notes

* GROUPING\_ID is an alias of GROUPING and uses the same argument order for its result bits.
* Deterministic: The current grouping set and argument order determine the bitmask.
* DISTINCT is not accepted.
* GROUPING is used with grouped queries. It is not available as a window function.
* Exact result: The supported argument count fits the signed 32-bit mask calculation.

## Examples

### Ordinary groups retain their grouping expression

```sql theme={"theme":{"light":"github-light","dark":"github-dark"}}
SELECT "value", CAST(grouping("value") AS BIGINT) AS grouping_flag FROM (VALUES (1),(2)) AS t("value") GROUP BY "value" ORDER BY "value";
```

| value | grouping\_flag |
| - | - |
| 1 | 0 |
| 2 | 0 |

### Alias calling form: GROUPING\_ID

```sql theme={"theme":{"light":"github-light","dark":"github-dark"}}
SELECT "value", CAST(grouping_id("value") AS BIGINT) AS grouping_flag FROM (VALUES (1),(2)) AS t("value") GROUP BY "value" ORDER BY "value";
```

| value | grouping\_flag |
| - | - |
| 1 | 0 |
| 2 | 0 |

## Limitations

* Supply 1 through 31 arguments. Calls with 32 or more arguments are rejected.
* Query tools report BIGINT, but some grouping-set queries return INTEGER. Cast the result to BIGINT when you need a consistent type.
* Use columns from the GROUP BY clause as arguments. Queries with more than 64 grouping columns can be rejected. Repeated grouping sets can also exceed the 64-position limit used to track grouping columns.
* DISTINCT, FILTER and window calling forms are unsupported.
* A GROUPING result can fail with a type mismatch. Cast it to BIGINT as shown in the example.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.