GoogleSQL for Bigtable supports the following general aggregate functions.
To learn about the syntax for aggregate function calls, see
[Aggregate function calls](https://docs.cloud.google.com/bigtable/docs/reference/sql/aggregate-function-calls.md.txt).

## Function list

| Name | Summary |
|---|---|
| [`ANY_VALUE`](https://docs.cloud.google.com/bigtable/docs/reference/sql/aggregate_functions.md.txt#any_value) | Gets an expression for some row. |
| [`APPROX_COUNT_DISTINCT`](https://docs.cloud.google.com/bigtable/docs/reference/sql/approximate_aggregate_functions.md.txt#approx_count_distinct) | Gets the approximate result for `COUNT(DISTINCT expression)`. For more information, see [Approximate aggregate functions](https://docs.cloud.google.com/bigtable/docs/reference/sql/approximate_aggregate_functions.md.txt). |
| [`APPROX_QUANTILES`](https://docs.cloud.google.com/bigtable/docs/reference/sql/approximate_aggregate_functions.md.txt#approx_quantiles) | Gets the approximate quantile boundaries. For more information, see [Approximate aggregate functions](https://docs.cloud.google.com/bigtable/docs/reference/sql/approximate_aggregate_functions.md.txt). |
| [`APPROX_TOP_COUNT`](https://docs.cloud.google.com/bigtable/docs/reference/sql/approximate_aggregate_functions.md.txt#approx_top_count) | Gets the approximate top elements and their approximate count. For more information, see [Approximate aggregate functions](https://docs.cloud.google.com/bigtable/docs/reference/sql/approximate_aggregate_functions.md.txt). |
| [`APPROX_TOP_SUM`](https://docs.cloud.google.com/bigtable/docs/reference/sql/approximate_aggregate_functions.md.txt#approx_top_sum) | Gets the approximate top elements and sum, based on the approximate sum of an assigned weight. For more information, see [Approximate aggregate functions](https://docs.cloud.google.com/bigtable/docs/reference/sql/approximate_aggregate_functions.md.txt). |
| [`ARRAY_AGG`](https://docs.cloud.google.com/bigtable/docs/reference/sql/aggregate_functions.md.txt#array_agg) | Gets an array of values. |
| [`ARRAY_CONCAT_AGG`](https://docs.cloud.google.com/bigtable/docs/reference/sql/aggregate_functions.md.txt#array_concat_agg) | Concatenates arrays and returns a single array as a result. |
| [`AVG`](https://docs.cloud.google.com/bigtable/docs/reference/sql/aggregate_functions.md.txt#avg) | Gets the average of non-`NULL` values. |
| [`BIT_AND`](https://docs.cloud.google.com/bigtable/docs/reference/sql/aggregate_functions.md.txt#bit_and) | Performs a bitwise AND operation on an expression. |
| [`BIT_OR`](https://docs.cloud.google.com/bigtable/docs/reference/sql/aggregate_functions.md.txt#bit_or) | Performs a bitwise OR operation on an expression. |
| [`BIT_XOR`](https://docs.cloud.google.com/bigtable/docs/reference/sql/aggregate_functions.md.txt#bit_xor) | Performs a bitwise XOR operation on an expression. |
| [`CORR`](https://docs.cloud.google.com/bigtable/docs/reference/sql/statistical_aggregate_functions.md.txt#corr) | Computes the Pearson coefficient of correlation of a set of number pairs. For more information, see [Statistical aggregate functions](https://docs.cloud.google.com/bigtable/docs/reference/sql/statistical_aggregate_functions.md.txt). |
| [`COUNT`](https://docs.cloud.google.com/bigtable/docs/reference/sql/aggregate_functions.md.txt#count) | Gets the number of rows in the input, or the number of rows with an expression evaluated to any value other than `NULL`. |
| [`COUNTIF`](https://docs.cloud.google.com/bigtable/docs/reference/sql/aggregate_functions.md.txt#countif) | Gets the number of `TRUE` values for an expression. |
| [`COVAR_POP`](https://docs.cloud.google.com/bigtable/docs/reference/sql/statistical_aggregate_functions.md.txt#covar_pop) | Computes the population covariance of a set of number pairs. For more information, see [Statistical aggregate functions](https://docs.cloud.google.com/bigtable/docs/reference/sql/statistical_aggregate_functions.md.txt). |
| [`COVAR_SAMP`](https://docs.cloud.google.com/bigtable/docs/reference/sql/statistical_aggregate_functions.md.txt#covar_samp) | Computes the sample covariance of a set of number pairs. For more information, see [Statistical aggregate functions](https://docs.cloud.google.com/bigtable/docs/reference/sql/statistical_aggregate_functions.md.txt). |
| [`LOGICAL_AND`](https://docs.cloud.google.com/bigtable/docs/reference/sql/aggregate_functions.md.txt#logical_and) | Gets the logical AND of all non-`NULL` expressions. |
| [`LOGICAL_OR`](https://docs.cloud.google.com/bigtable/docs/reference/sql/aggregate_functions.md.txt#logical_or) | Gets the logical OR of all non-`NULL` expressions. |
| [`MAX`](https://docs.cloud.google.com/bigtable/docs/reference/sql/aggregate_functions.md.txt#max) | Gets the maximum non-`NULL` value. |
| [`MIN`](https://docs.cloud.google.com/bigtable/docs/reference/sql/aggregate_functions.md.txt#min) | Gets the minimum non-`NULL` value. |
| [`PERCENTILE_CONT`](https://docs.cloud.google.com/bigtable/docs/reference/sql/navigation_functions.md.txt#percentile_cont) | Computes the specified percentile for a value, using linear interpolation. For more information, see [Navigation functions](https://docs.cloud.google.com/bigtable/docs/reference/sql/navigation_functions.md.txt). |
| [`PERCENTILE_DISC`](https://docs.cloud.google.com/bigtable/docs/reference/sql/navigation_functions.md.txt#percentile_disc) | Computes the specified percentile for a discrete value. For more information, see [Navigation functions](https://docs.cloud.google.com/bigtable/docs/reference/sql/navigation_functions.md.txt). |
| [`ST_EXTENT`](https://docs.cloud.google.com/bigtable/docs/reference/sql/geography_functions.md.txt#st_extent) | Gets the bounding box for a group of `GEOGRAPHY` values. For more information, see [Geography functions](https://docs.cloud.google.com/bigtable/docs/reference/sql/geography_functions.md.txt). |
| [`ST_UNION_AGG`](https://docs.cloud.google.com/bigtable/docs/reference/sql/geography_functions.md.txt#st_union_agg) | Aggregates over `GEOGRAPHY` values and gets their point set union. For more information, see [Geography functions](https://docs.cloud.google.com/bigtable/docs/reference/sql/geography_functions.md.txt). |
| [`STDDEV`](https://docs.cloud.google.com/bigtable/docs/reference/sql/statistical_aggregate_functions.md.txt#stddev) | An alias of the `STDDEV_SAMP` function. For more information, see [Statistical aggregate functions](https://docs.cloud.google.com/bigtable/docs/reference/sql/statistical_aggregate_functions.md.txt). |
| [`STDDEV_POP`](https://docs.cloud.google.com/bigtable/docs/reference/sql/statistical_aggregate_functions.md.txt#stddev_pop) | Computes the population (biased) standard deviation of the values. For more information, see [Statistical aggregate functions](https://docs.cloud.google.com/bigtable/docs/reference/sql/statistical_aggregate_functions.md.txt). |
| [`STDDEV_SAMP`](https://docs.cloud.google.com/bigtable/docs/reference/sql/statistical_aggregate_functions.md.txt#stddev_samp) | Computes the sample (unbiased) standard deviation of the values. For more information, see [Statistical aggregate functions](https://docs.cloud.google.com/bigtable/docs/reference/sql/statistical_aggregate_functions.md.txt). |
| [`STRING_AGG`](https://docs.cloud.google.com/bigtable/docs/reference/sql/aggregate_functions.md.txt#string_agg) | Concatenates non-`NULL` `STRING` or `BYTES` values. |
| [`SUM`](https://docs.cloud.google.com/bigtable/docs/reference/sql/aggregate_functions.md.txt#sum) | Gets the sum of non-`NULL` values. |
| [`VAR_POP`](https://docs.cloud.google.com/bigtable/docs/reference/sql/statistical_aggregate_functions.md.txt#var_pop) | Computes the population (biased) variance of the values. For more information, see [Statistical aggregate functions](https://docs.cloud.google.com/bigtable/docs/reference/sql/statistical_aggregate_functions.md.txt). |
| [`VAR_SAMP`](https://docs.cloud.google.com/bigtable/docs/reference/sql/statistical_aggregate_functions.md.txt#var_samp) | Computes the sample (unbiased) variance of the values. For more information, see [Statistical aggregate functions](https://docs.cloud.google.com/bigtable/docs/reference/sql/statistical_aggregate_functions.md.txt). |
| [`VARIANCE`](https://docs.cloud.google.com/bigtable/docs/reference/sql/statistical_aggregate_functions.md.txt#variance) | An alias of `VAR_SAMP`. For more information, see [Statistical aggregate functions](https://docs.cloud.google.com/bigtable/docs/reference/sql/statistical_aggregate_functions.md.txt). |

## `ANY_VALUE`

```googlesql
ANY_VALUE(
  expression
)
[ OVER over_clause ]

over_clause:
  { named_window | ( [ window_specification ] ) }

window_specification:
  [ named_window ]
  [ PARTITION BY partition_expression [, ...] ]
  [ ORDER BY expression [ { ASC | DESC }  ] [, ...] ]
  [ window_frame_clause ]

```

**Description**

Returns `expression` for some row chosen from the group. Which row is chosen is
nondeterministic, not random. Returns `NULL` when the input produces no
rows. Returns `NULL` when `expression`
is
`NULL` for all rows in the group.

If `expression` contains any non-NULL values,
then `ANY_VALUE` behaves as if
`IGNORE NULLS` is specified;
rows for which `expression` is `NULL` aren't considered and won't be
selected.

To learn more about the optional aggregate clauses that you can pass
into this function, see
[Aggregate function calls](https://docs.cloud.google.com/bigtable/docs/reference/sql/aggregate-function-calls.md.txt).

To learn more about the `OVER` clause and how to use it, see
[Window function calls](https://docs.cloud.google.com/bigtable/docs/reference/sql/window-function-calls.md.txt).

**Supported Argument Types**

Any

**Returned Data Types**

Matches the input data type.

**Examples**

```googlesql
SELECT ANY_VALUE(fruit) as any_value
FROM UNNEST(["apple", "banana", "pear"]) as fruit;

/*---+
 | any_value |
 +---+
 | apple     |
 +---*/
```

```googlesql
SELECT
  fruit,
  ANY_VALUE(fruit) OVER (ORDER BY LENGTH(fruit) ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS any_value
FROM UNNEST(["apple", "banana", "pear"]) as fruit;

/*---+---+
 | fruit  | any_value |
 +---+---+
 | pear   | pear      |
 | apple  | pear      |
 | banana | apple     |
 +---+---*/
```

## `ARRAY_AGG`

```googlesql
ARRAY_AGG(
  [ DISTINCT ]
  expression
  [ { IGNORE | RESPECT } NULLS ]
)
[ OVER over_clause ]

over_clause:
  { named_window | ( [ window_specification ] ) }

window_specification:
  [ named_window ]
  [ PARTITION BY partition_expression [, ...] ]
  [ ORDER BY expression [ { ASC | DESC }  ] [, ...] ]
  [ window_frame_clause ]

```

**Description**

Returns an ARRAY of `expression` values. The order of elements
in the returned array is arbitrary.

To learn more about the optional aggregate clauses that you can pass
into this function, see
[Aggregate function calls](https://docs.cloud.google.com/bigtable/docs/reference/sql/aggregate-function-calls.md.txt).

If this function is used with the `OVER` clause, it's part of a
window function call. In a window function call,
aggregate function clauses can't be used.
To learn more about the `OVER` clause and how to use it, see
[Window function calls](https://docs.cloud.google.com/bigtable/docs/reference/sql/window-function-calls.md.txt).

**Supported Argument Types**

All data types except ARRAY.

**Returned Data Types**

ARRAY

If there are zero input rows, this function returns `NULL`.

**Examples**

```googlesql
SELECT ARRAY_AGG(x) AS array_agg FROM UNNEST([2, 1,-2, 3, -2, 1, 2]) AS x;

/*---+
 | array_agg               |
 +---+
 | [2, 1, -2, 3, -2, 1, 2] |
 +---*/
```

```googlesql
SELECT ARRAY_AGG(DISTINCT x) AS array_agg
FROM UNNEST([2, 1, -2, 3, -2, 1, 2]) AS x;

/*---+
 | array_agg     |
 +---+
 | [2, 1, -2, 3] |
 +---*/
```

```googlesql
SELECT ARRAY_AGG(x IGNORE NULLS) AS array_agg
FROM UNNEST([NULL, 1, -2, 3, -2, 1, NULL]) AS x;

/*---+
 | array_agg         |
 +---+
 | [1, -2, 3, -2, 1] |
 +---*/
```

```googlesql
SELECT x, ARRAY_AGG(y) AS array_agg
FROM
  UNNEST(
    ARRAY<STRUCT<x INT64, y STRING>>[(1, 'a'), (1, 'b'), (2, 'a'), (2, 'c')])
GROUP BY x;

/*---+
 | x | array_agg |
 +---+
 | 1 | [a, b]    |
 | 2 | [a, c]    |
 +---*/
```

## `ARRAY_CONCAT_AGG`

```googlesql
ARRAY_CONCAT_AGG(
  [ DISTINCT ]
  expression
)
[ OVER over_clause ]

over_clause:
  { named_window | ( [ window_specification ] ) }

window_specification:
  [ named_window ]
  [ PARTITION BY partition_expression [, ...] ]
  [ ORDER BY expression [ { ASC | DESC }  ] [, ...] ]
  [ window_frame_clause ]

```

**Description**

Concatenates elements from `expression` of type `ARRAY`, returning a single
array as a result.

This function ignores `NULL` input arrays, but respects the `NULL` elements in
non-`NULL` input arrays. Returns `NULL` if there are zero input rows or
`expression` evaluates to `NULL` for all rows.

To learn more about the optional aggregate clauses that you can pass
into this function, see
[Aggregate function calls](https://docs.cloud.google.com/bigtable/docs/reference/sql/aggregate-function-calls.md.txt).

If the optional `DISTINCT` clause is used, it only applies to complete arrays,
not elements within an array.

**Supported Argument Types**

`ARRAY`

**Returned Data Types**

`ARRAY`

**Examples**

```googlesql
SELECT FORMAT("%T", ARRAY_CONCAT_AGG(x)) AS array_concat_agg FROM (
  SELECT [NULL, 1, 2, 3, 4] AS x
  UNION ALL SELECT NULL
  UNION ALL SELECT [5, 6]
  UNION ALL SELECT [7, 8, 9]
);

/*---+
 | array_concat_agg                  |
 +---+
 | [NULL, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
 +---*/
```

```googlesql
SELECT FORMAT("%T", ARRAY_CONCAT_AGG(DISTINCT x)) AS array_concat_agg FROM (
  SELECT [1, 2, 3, 4, 4] AS x
  UNION ALL SELECT [5, 6]
  UNION ALL SELECT [5, 6]
);

/*---+
 | array_concat_agg                  |
 +---+
 | [1, 2, 3, 4, 4, 5, 6]             |
 +---*/
```

## `AVG`

```googlesql
AVG(
  [ DISTINCT ]
  expression
)
[ OVER over_clause ]

over_clause:
  { named_window | ( [ window_specification ] ) }

window_specification:
  [ named_window ]
  [ PARTITION BY partition_expression [, ...] ]
  [ ORDER BY expression [ { ASC | DESC }  ] [, ...] ]
  [ window_frame_clause ]

```

**Description**

Returns the average of non-`NULL` values in an aggregated group.

To learn more about the optional aggregate clauses that you can pass
into this function, see
[Aggregate function calls](https://docs.cloud.google.com/bigtable/docs/reference/sql/aggregate-function-calls.md.txt).

If this function is used with the `OVER` clause, it's part of a
window function call. In a window function call,
aggregate function clauses can't be used.
To learn more about the `OVER` clause and how to use it, see
[Window function calls](https://docs.cloud.google.com/bigtable/docs/reference/sql/window-function-calls.md.txt).

Caveats:

- If the aggregated group is empty or the argument is `NULL` for all rows in the group, returns `NULL`.
- If the argument is `NaN` for any row in the group, returns `NaN`.
- If the argument is `[+|-]Infinity` for any row in the group, returns either `[+|-]Infinity` or `NaN`.
- If there is numeric overflow, produces an error.
- If a [floating-point type](https://docs.cloud.google.com/bigtable/docs/reference/sql/data-types.md.txt#floating_point_types) is returned, the result is [non-deterministic](https://docs.cloud.google.com/bigtable/docs/reference/sql/data-types.md.txt#floating_point_semantics), which means you might receive a different result each time you use this function.

**Supported Argument Types**

- Any numeric input type

**Returned Data Types**

| INPUT | `INT64` | `FLOAT32` | `FLOAT64` |
| OUTPUT | `FLOAT64` | `FLOAT64` | `FLOAT64` |
|---|---|---|---|

**Examples**

```googlesql
SELECT AVG(x) as avg
FROM UNNEST([0, 2, 4, 4, 5]) as x;

/*---+
 | avg |
 +---+
 | 3   |
 +---*/
```

```googlesql
SELECT AVG(DISTINCT x) AS avg
FROM UNNEST([0, 2, 4, 4, 5]) AS x;

/*---+
 | avg  |
 +---+
 | 2.75 |
 +---*/
```

```googlesql
SELECT
  x,
  AVG(x) OVER (ORDER BY x ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS avg
FROM UNNEST([0, 2, NULL, 4, 4, 5]) AS x;

/*---+---+
 | x    | avg  |
 +---+---+
 | NULL | NULL |
 | 0    | 0    |
 | 2    | 1    |
 | 4    | 3    |
 | 4    | 4    |
 | 5    | 4.5  |
 +---+---*/
```

## `BIT_AND`

```googlesql
BIT_AND(
  [ DISTINCT ]
  expression
)
[ OVER over_clause ]

over_clause:
  { named_window | ( [ window_specification ] ) }

window_specification:
  [ named_window ]
  [ PARTITION BY partition_expression [, ...] ]
  [ ORDER BY expression [ { ASC | DESC }  ] [, ...] ]
  [ window_frame_clause ]

```

**Description**

Performs a bitwise AND operation on `expression` and returns the result.

To learn more about the optional aggregate clauses that you can pass
into this function, see
[Aggregate function calls](https://docs.cloud.google.com/bigtable/docs/reference/sql/aggregate-function-calls.md.txt).

**Supported Argument Types**

- INT64

**Returned Data Types**

INT64

**Examples**

```googlesql
SELECT BIT_AND(x) as bit_and FROM UNNEST([0xF001, 0x00A1]) as x;

/*---+
 | bit_and |
 +---+
 | 1       |
 +---*/
```

## `BIT_OR`

```googlesql
BIT_OR(
  [ DISTINCT ]
  expression
)
[ OVER over_clause ]

over_clause:
  { named_window | ( [ window_specification ] ) }

window_specification:
  [ named_window ]
  [ PARTITION BY partition_expression [, ...] ]
  [ ORDER BY expression [ { ASC | DESC }  ] [, ...] ]
  [ window_frame_clause ]

```

**Description**

Performs a bitwise OR operation on `expression` and returns the result.

To learn more about the optional aggregate clauses that you can pass
into this function, see
[Aggregate function calls](https://docs.cloud.google.com/bigtable/docs/reference/sql/aggregate-function-calls.md.txt).

**Supported Argument Types**

- INT64

**Returned Data Types**

INT64

**Examples**

```googlesql
SELECT BIT_OR(x) as bit_or FROM UNNEST([0xF001, 0x00A1]) as x;

/*---+
 | bit_or |
 +---+
 | 61601  |
 +---*/
```

## `BIT_XOR`

```googlesql
BIT_XOR(
  [ DISTINCT ]
  expression
)
```

**Description**

Performs a bitwise XOR operation on `expression` and returns the result.

To learn more about the optional aggregate clauses that you can pass
into this function, see
[Aggregate function calls](https://docs.cloud.google.com/bigtable/docs/reference/sql/aggregate-function-calls.md.txt).

**Supported Argument Types**

- INT64

**Returned Data Types**

INT64

**Examples**

```googlesql
SELECT BIT_XOR(x) AS bit_xor FROM UNNEST([5678, 1234]) AS x;

/*---+
 | bit_xor |
 +---+
 | 4860    |
 +---*/
```

```googlesql
SELECT BIT_XOR(x) AS bit_xor FROM UNNEST([1234, 5678, 1234]) AS x;

/*---+
 | bit_xor |
 +---+
 | 5678    |
 +---*/
```

```googlesql
SELECT BIT_XOR(DISTINCT x) AS bit_xor FROM UNNEST([1234, 5678, 1234]) AS x;

/*---+
 | bit_xor |
 +---+
 | 4860    |
 +---*/
```

## `COUNT`

```googlesql
COUNT(*)
[ OVER over_clause ]
```

```googlesql
COUNT(
  [ DISTINCT ]
  expression
)
[ OVER over_clause ]

over_clause:
  { named_window | ( [ window_specification ] ) }

window_specification:
  [ named_window ]
  [ PARTITION BY partition_expression [, ...] ]
  [ ORDER BY expression [ { ASC | DESC }  ] [, ...] ]
  [ window_frame_clause ]

```

**Description**

Gets the number of rows in the input or the number of rows with an
expression evaluated to any value other than `NULL`.

> [!NOTE]
> **Note:** If you're querying a large dataset, you can compute results faster and save resources by using [HLL++ functions](https://docs.cloud.google.com/bigtable/docs/reference/sql/hll_functions.md.txt) for approximate distinct counts. For more information, see [Sketches](https://docs.cloud.google.com/bigtable/docs/reference/sql/sketches.md.txt).

**Definitions**

- `*`: Use this value to get the number of all rows in the input.
- `expression`: A value of any data type that represents the expression to evaluate. If `DISTINCT` is present, `expression` can only be a data type that is [groupable](https://docs.cloud.google.com/bigtable/docs/reference/sql/data-types.md.txt#groupable_data_types).
- Optional aggregate clauses: To learn more about the optional aggregate clauses that you can pass into this function, see [Aggregate function calls](https://docs.cloud.google.com/bigtable/docs/reference/sql/aggregate-function-calls.md.txt).

<!-- -->

- `OVER`: To learn more about the `OVER` clause and how to use it, see
  [Window function calls](https://docs.cloud.google.com/bigtable/docs/reference/sql/window-function-calls.md.txt).

  > [!NOTE]
  > **Note:** If this function is used with the `OVER` clause, it's part of a window function call. In a window function call, aggregate function clauses can't be used.

**Details**

To count the number of distinct values of an expression for which a
certain condition is satisfied, you can use the following recipe:

```googlesql
COUNT(DISTINCT IF(condition, expression, NULL))
```

`IF` returns the value of `expression` if `condition` is `TRUE`, or
`NULL` otherwise. The surrounding `COUNT(DISTINCT ...)` ignores the `NULL`
values, so it counts only the distinct values of `expression` for which
`condition` is `TRUE`.

To count the number of non-distinct values of an expression for which a
certain condition is satisfied, consider using the
[`COUNTIF`](https://docs.cloud.google.com/bigtable/docs/reference/sql/aggregate_functions.md.txt#countif) function.

This function with `DISTINCT` supports specifying [collation](https://docs.cloud.google.com/bigtable/docs/reference/sql/collation-concepts.md.txt).

**Return type**

`INT64`

**Examples**

You can use the `COUNT` function to return the number of rows in a table or the
number of distinct values of an expression. For example:

```googlesql
SELECT
  COUNT(*) AS count_star,
  COUNT(DISTINCT x) AS count_dist_x
FROM UNNEST([1, 4, 4, 5]) AS x;

/*---+---+
 | count_star | count_dist_x |
 +---+---+
 | 4          | 3            |
 +---+---*/
```

```googlesql
SELECT
  x,
  COUNT(*) OVER (PARTITION BY MOD(x, 3)) AS count_star,
  COUNT(x) OVER (PARTITION BY MOD(x, 3)) AS count_x
FROM UNNEST([1, 4, 4, 5]) AS x;

/*---+---+---+
 | x    | count_star | count_x |
 +---+---+---+
 | 1    | 3          | 3       |
 | 4    | 3          | 3       |
 | 4    | 3          | 3       |
 | 5    | 1          | 1       |
 +---+---+---*/
```

```googlesql
SELECT
  x,
  COUNT(*) OVER (PARTITION BY MOD(x, 3)) AS count_star,
  COUNT(x) OVER (PARTITION BY MOD(x, 3)) AS count_x
FROM UNNEST([1, 4, NULL, 4, 5]) AS x;

/*---+---+---+
 | x    | count_star | count_x |
 +---+---+---+
 | NULL | 1          | 0       |
 | 1    | 3          | 3       |
 | 4    | 3          | 3       |
 | 4    | 3          | 3       |
 | 5    | 1          | 1       |
 +---+---+---*/
```

The following query counts the number of distinct positive values of `x`:

```googlesql
SELECT COUNT(DISTINCT IF(x > 0, x, NULL)) AS distinct_positive
FROM UNNEST([1, -2, 4, 1, -5, 4, 1, 3, -6, 1]) AS x;

/*---+
 | distinct_positive |
 +---+
 | 3                 |
 +---*/
```

The following query counts the number of distinct dates on which a certain kind
of event occurred:

```googlesql
SELECT
  COUNT(DISTINCT IF(event_type = 'FAILURE', event_date, NULL))
    AS distinct_dates_with_failures
FROM
  UNNEST(
    ARRAY<STRUCT<event_date DATE, event_type STRING>>[
      (DATE '2021-01-01', 'SUCCESS'),
      (DATE '2021-01-02', 'SUCCESS'),
      (DATE '2021-01-02', 'FAILURE'),
      (DATE '2021-01-03', 'SUCCESS'),
      (DATE '2021-01-04', 'FAILURE'),
      (DATE '2021-01-04', 'FAILURE')]) AS Events;

/*---+
 | distinct_dates_with_failures |
 +---+
 | 2                            |
 +---*/
```

The following query counts the number of distinct `id`s that exist in both
the `customers` and `vendor` tables:

```googlesql
SELECT
  COUNT(
    DISTINCT
      IF(
        id IN (
          SELECT id
          FROM
            UNNEST(
              ARRAY<STRUCT<id INT64, team STRING>>[
                (1934, 'a'),
                (2991, 'b'),
                (3988, 'c')]) AS customers
        ),
        id,
        NULL)) AS result
FROM
  UNNEST(
    ARRAY<STRUCT<id INT64, team STRING>>[
      (1934, 'd'),
      (2991, 'e'),
      (4366, 'f')]) AS vendors

/*---+
 | result |
 +---+
 | 2      |
 +---*/
```

## `COUNTIF`

```googlesql
COUNTIF(
  [ DISTINCT ]
  expression
)
[ OVER over_clause ]

over_clause:
  { named_window | ( [ window_specification ] ) }

window_specification:
  [ named_window ]
  [ PARTITION BY partition_expression [, ...] ]
  [ ORDER BY expression [ { ASC | DESC }  ] [, ...] ]
  [ window_frame_clause ]

```

**Description**

Gets the number of `TRUE` values for an expression.

**Definitions**

- `expression`: A `BOOL` value that represents the expression to evaluate.
- Optional aggregate clauses: To learn more about the optional aggregate clauses that you can pass into this function, see [Aggregate function calls](https://docs.cloud.google.com/bigtable/docs/reference/sql/aggregate-function-calls.md.txt).

<!-- -->

- `OVER`: To learn more about the `OVER` clause and how to use it, see [Window function calls](https://docs.cloud.google.com/bigtable/docs/reference/sql/window-function-calls.md.txt).

**Details**

The function signature `COUNTIF(DISTINCT ...)` is generally not useful. If you
would like to use `DISTINCT`, use `COUNT` with `DISTINCT IF`. For more
information, see the [`COUNT`](https://docs.cloud.google.com/bigtable/docs/reference/sql/aggregate_functions.md.txt#count) function.

**Return type**

`INT64`

**Examples**

```googlesql
SELECT COUNTIF(x<0) AS num_negative, COUNTIF(x>0) AS num_positive
FROM UNNEST([5, -2, 3, 6, -10, -7, 4, 0]) AS x;

/*---+---+
 | num_negative | num_positive |
 +---+---+
 | 3            | 4            |
 +---+---*/
```

```googlesql
SELECT
  x,
  COUNTIF(x<0) OVER (ORDER BY ABS(x) ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS num_negative
FROM UNNEST([5, -2, 3, 6, -10, NULL, -7, 4, 0]) AS x;

/*---+---+
 | x    | num_negative |
 +---+---+
 | NULL | 0            |
 | 0    | 1            |
 | -2   | 1            |
 | 3    | 1            |
 | 4    | 0            |
 | 5    | 0            |
 | 6    | 1            |
 | -7   | 2            |
 | -10  | 2            |
 +---+---*/
```

## `LOGICAL_AND`

```googlesql
LOGICAL_AND(
  [ DISTINCT ]
  expression
)
[ OVER over_clause ]

over_clause:
  { named_window | ( [ window_specification ] ) }

window_specification:
  [ named_window ]
  [ PARTITION BY partition_expression [, ...] ]
  [ ORDER BY expression [ { ASC | DESC }  ] [, ...] ]
  [ window_frame_clause ]

```

**Description**

Returns the logical AND of all non-`NULL` expressions. Returns `NULL` if there
are zero input rows or `expression` evaluates to `NULL` for all rows.

To learn more about the optional aggregate clauses that you can pass
into this function, see
[Aggregate function calls](https://docs.cloud.google.com/bigtable/docs/reference/sql/aggregate-function-calls.md.txt).

If this function is used with the `OVER` clause, it's part of a
window function call. In a window function call,
aggregate function clauses can't be used.
To learn more about the `OVER` clause and how to use it, see
[Window function calls](https://docs.cloud.google.com/bigtable/docs/reference/sql/window-function-calls.md.txt).

**Supported Argument Types**

`BOOL`

**Return Data Types**

`BOOL`

**Examples**

`LOGICAL_AND` returns `FALSE` because not all of the values in the array are
less than 3.

```googlesql
SELECT LOGICAL_AND(x < 3) AS logical_and FROM UNNEST([1, 2, 4]) AS x;

/*---+
 | logical_and |
 +---+
 | FALSE       |
 +---*/
```

## `LOGICAL_OR`

```googlesql
LOGICAL_OR(
  [ DISTINCT ]
  expression
)
[ OVER over_clause ]

over_clause:
  { named_window | ( [ window_specification ] ) }

window_specification:
  [ named_window ]
  [ PARTITION BY partition_expression [, ...] ]
  [ ORDER BY expression [ { ASC | DESC }  ] [, ...] ]
  [ window_frame_clause ]

```

**Description**

Returns the logical OR of all non-`NULL` expressions. Returns `NULL` if there
are zero input rows or `expression` evaluates to `NULL` for all rows.

To learn more about the optional aggregate clauses that you can pass
into this function, see
[Aggregate function calls](https://docs.cloud.google.com/bigtable/docs/reference/sql/aggregate-function-calls.md.txt).

If this function is used with the `OVER` clause, it's part of a
window function call. In a window function call,
aggregate function clauses can't be used.
To learn more about the `OVER` clause and how to use it, see
[Window function calls](https://docs.cloud.google.com/bigtable/docs/reference/sql/window-function-calls.md.txt).

**Supported Argument Types**

`BOOL`

**Return Data Types**

`BOOL`

**Examples**

`LOGICAL_OR` returns `TRUE` because at least one of the values in the array is
less than 3.

```googlesql
SELECT LOGICAL_OR(x < 3) AS logical_or FROM UNNEST([1, 2, 4]) AS x;

/*---+
 | logical_or |
 +---+
 | TRUE       |
 +---*/
```

## `MAX`

```googlesql
MAX(
  expression
)
[ OVER over_clause ]

over_clause:
  { named_window | ( [ window_specification ] ) }

window_specification:
  [ named_window ]
  [ PARTITION BY partition_expression [, ...] ]
  [ ORDER BY expression [ { ASC | DESC }  ] [, ...] ]
  [ window_frame_clause ]

```

**Description**

Returns the maximum non-`NULL` value in an aggregated group.

Caveats:

- If the aggregated group is empty or the argument is `NULL` for all rows in the group, returns `NULL`.
- If the argument is `NaN` for any row in the group, returns `NaN`.

To learn more about the optional aggregate clauses that you can pass
into this function, see
[Aggregate function calls](https://docs.cloud.google.com/bigtable/docs/reference/sql/aggregate-function-calls.md.txt).

To learn more about the `OVER` clause and how to use it, see
[Window function calls](https://docs.cloud.google.com/bigtable/docs/reference/sql/window-function-calls.md.txt).

This function supports specifying [collation](https://docs.cloud.google.com/bigtable/docs/reference/sql/collation-concepts.md.txt).

**Supported Argument Types**

Any [orderable data type](https://docs.cloud.google.com/bigtable/docs/reference/sql/data-types.md.txt#data_type_properties).

**Return Data Types**

The data type of the input values.

**Examples**

```googlesql
SELECT MAX(x) AS max
FROM UNNEST([8, 37, 55, 4]) AS x;

/*---+
 | max |
 +---+
 | 55  |
 +---*/
```

```googlesql
SELECT x, MAX(x) OVER (PARTITION BY MOD(x, 2)) AS max
FROM UNNEST([8, NULL, 37, 55, NULL, 4]) AS x;

/*---+---+
 | x    | max  |
 +---+---+
 | NULL | NULL |
 | NULL | NULL |
 | 8    | 8    |
 | 4    | 8    |
 | 37   | 55   |
 | 55   | 55   |
 +---+---*/
```

## `MIN`

```googlesql
MIN(
  expression
)
[ OVER over_clause ]

over_clause:
  { named_window | ( [ window_specification ] ) }

window_specification:
  [ named_window ]
  [ PARTITION BY partition_expression [, ...] ]
  [ ORDER BY expression [ { ASC | DESC }  ] [, ...] ]
  [ window_frame_clause ]

```

**Description**

Returns the minimum non-`NULL` value in an aggregated group.

Caveats:

- If the aggregated group is empty or the argument is `NULL` for all rows in the group, returns `NULL`.
- If the argument is `NaN` for any row in the group, returns `NaN`.

To learn more about the optional aggregate clauses that you can pass
into this function, see
[Aggregate function calls](https://docs.cloud.google.com/bigtable/docs/reference/sql/aggregate-function-calls.md.txt).

To learn more about the `OVER` clause and how to use it, see
[Window function calls](https://docs.cloud.google.com/bigtable/docs/reference/sql/window-function-calls.md.txt).

This function supports specifying [collation](https://docs.cloud.google.com/bigtable/docs/reference/sql/collation-concepts.md.txt).

**Supported Argument Types**

Any [orderable data type](https://docs.cloud.google.com/bigtable/docs/reference/sql/data-types.md.txt#data_type_properties).

**Return Data Types**

The data type of the input values.

**Examples**

```googlesql
SELECT MIN(x) AS min
FROM UNNEST([8, 37, 4, 55]) AS x;

/*---+
 | min |
 +---+
 | 4   |
 +---*/
```

```googlesql
SELECT x, MIN(x) OVER (PARTITION BY MOD(x, 2)) AS min
FROM UNNEST([8, NULL, 37, 4, NULL, 55]) AS x;

/*---+---+
 | x    | min  |
 +---+---+
 | NULL | NULL |
 | NULL | NULL |
 | 8    | 4    |
 | 4    | 4    |
 | 37   | 37   |
 | 55   | 37   |
 +---+---*/
```

## `STRING_AGG`

```googlesql
STRING_AGG(
  [ DISTINCT ]
  expression [, delimiter]
)
[ OVER over_clause ]

over_clause:
  { named_window | ( [ window_specification ] ) }

window_specification:
  [ named_window ]
  [ PARTITION BY partition_expression [, ...] ]
  [ ORDER BY expression [ { ASC | DESC }  ] [, ...] ]
  [ window_frame_clause ]

```

**Description**

Returns a value (either `STRING` or `BYTES`) obtained by concatenating
non-`NULL` values. Returns `NULL` if there are zero input rows or `expression`
evaluates to `NULL` for all rows.

If a `delimiter` is specified, concatenated values are separated by that
delimiter; otherwise, a comma is used as a delimiter.

To learn more about the optional aggregate clauses that you can pass
into this function, see
[Aggregate function calls](https://docs.cloud.google.com/bigtable/docs/reference/sql/aggregate-function-calls.md.txt).

If this function is used with the `OVER` clause, it's part of a
window function call. In a window function call,
aggregate function clauses can't be used.
To learn more about the `OVER` clause and how to use it, see
[Window function calls](https://docs.cloud.google.com/bigtable/docs/reference/sql/window-function-calls.md.txt).

**Supported Argument Types**

Either `STRING` or `BYTES`.

**Return Data Types**

Either `STRING` or `BYTES`.

**Examples**

```googlesql
SELECT STRING_AGG(fruit) AS string_agg
FROM UNNEST(["apple", NULL, "pear", "banana", "pear"]) AS fruit;

/*---+
 | string_agg             |
 +---+
 | apple,pear,banana,pear |
 +---*/
```

```googlesql
SELECT STRING_AGG(fruit, " & ") AS string_agg
FROM UNNEST(["apple", "pear", "banana", "pear"]) AS fruit;

/*---+
 | string_agg                   |
 +---+
 | apple & pear & banana & pear |
 +---*/
```

```googlesql
SELECT STRING_AGG(DISTINCT fruit, " & ") AS string_agg
FROM UNNEST(["apple", "pear", "banana", "pear"]) AS fruit;

/*---+
 | string_agg            |
 +---+
 | apple & pear & banana |
 +---*/
```

```googlesql
SELECT
  fruit,
  STRING_AGG(fruit, " & ") OVER (ORDER BY LENGTH(fruit)) AS string_agg
FROM UNNEST(["apple", NULL, "pear", "banana", "pear"]) AS fruit;

/*---+---+
 | fruit  | string_agg                   |
 +---+---+
 | NULL   | NULL                         |
 | pear   | pear & pear                  |
 | pear   | pear & pear                  |
 | apple  | pear & pear & apple          |
 | banana | pear & pear & apple & banana |
 +---+---*/
```

## `SUM`

```googlesql
SUM(
  [ DISTINCT ]
  expression
)
[ OVER over_clause ]

over_clause:
  { named_window | ( [ window_specification ] ) }

window_specification:
  [ named_window ]
  [ PARTITION BY partition_expression [, ...] ]
  [ ORDER BY expression [ { ASC | DESC }  ] [, ...] ]
  [ window_frame_clause ]

```

**Description**

Returns the sum of non-`NULL` values in an aggregated group.
Supports the `SAFE.` prefix when used
with the `OVER` clause as a window function.

To learn more about the optional aggregate clauses that you can pass
into this function, see
[Aggregate function calls](https://docs.cloud.google.com/bigtable/docs/reference/sql/aggregate-function-calls.md.txt).

If this function is used with the `OVER` clause, it's part of a
window function call. In a window function call,
aggregate function clauses can't be used.
To learn more about the `OVER` clause and how to use it, see
[Window function calls](https://docs.cloud.google.com/bigtable/docs/reference/sql/window-function-calls.md.txt).

Caveats:

- If the aggregated group is empty or the argument is `NULL` for all rows in the group, returns `NULL`.
- If the argument is `NaN` for any row in the group, returns `NaN`.
- If the argument is `[+|-]Infinity` for any row in the group, returns either `[+|-]Infinity` or `NaN`.
- If there is numeric overflow, produces an error.
- If a [floating-point type](https://docs.cloud.google.com/bigtable/docs/reference/sql/data-types.md.txt#floating_point_types) is returned, the result is [non-deterministic](https://docs.cloud.google.com/bigtable/docs/reference/sql/data-types.md.txt#floating_point_semantics), which means you might receive a different result each time you use this function.

**Supported Argument Types**

- Any supported numeric data type

**Return Data Types**

| INPUT | `INT64` | `FLOAT32` | `FLOAT64` |
| OUTPUT | `INT64` | `FLOAT64` | `FLOAT64` |
|---|---|---|---|

**Examples**

```googlesql
SELECT SUM(x) AS sum
FROM UNNEST([1, 2, 3, 4, 5, 4, 3, 2, 1]) AS x;

/*---+
 | sum |
 +---+
 | 25  |
 +---*/
```

```googlesql
SELECT SUM(DISTINCT x) AS sum
FROM UNNEST([1, 2, 3, 4, 5, 4, 3, 2, 1]) AS x;

/*---+
 | sum |
 +---+
 | 15  |
 +---*/
```

```googlesql
SELECT
  x,
  SUM(x) OVER (PARTITION BY MOD(x, 3)) AS sum
FROM UNNEST([1, 2, 3, 4, 5, 4, 3, 2, 1]) AS x;

/*---+---+
 | x | sum |
 +---+---+
 | 3 | 6   |
 | 3 | 6   |
 | 1 | 10  |
 | 4 | 10  |
 | 4 | 10  |
 | 1 | 10  |
 | 2 | 9   |
 | 5 | 9   |
 | 2 | 9   |
 +---+---*/
```

```googlesql
SELECT SUM(x) AS sum
FROM UNNEST([]) AS x;

/*---+
 | sum  |
 +---+
 | NULL |
 +---*/
```