ClickHouse/docs/en/sql-reference/aggregate-functions/reference/any.md

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

61 lines
2.4 KiB
Markdown
Raw Normal View History

---
2022-08-28 14:53:34 +00:00
slug: /en/sql-reference/aggregate-functions/reference/any
sidebar_position: 102
---
2022-06-02 10:55:18 +00:00
# any
Selects the first encountered value of a column, ignoring any `NULL` values.
2024-05-04 14:20:58 +00:00
**Syntax**
2024-05-04 14:20:58 +00:00
```sql
any(column) [RESPECT NULLS]
2024-05-04 14:20:58 +00:00
```
Aliases: `any_value`, [`first_value`](../reference/first_value.md).
**Parameters**
- `column`: The column name.
**Returned value**
:::note
By default, the `anyLast` function never returns `NULL`. However, it supports the `RESPECT NULLS` modifier after the function name. Using this modifier will ensure the function selects the first value passed, regardless of whether it is `NULL` or not.
Alias: `anyRespectNulls`
:::
2024-05-04 14:20:58 +00:00
:::note
The return type of the function is the same as the input, except for LowCardinality which is discarded. This means that given no rows as input it will return the default value of that type (0 for integers, or Null for a Nullable() column). You might use the `-OrNull` [combinator](../../../sql-reference/aggregate-functions/combinators.md) ) to modify this behaviour.
2024-05-04 14:20:58 +00:00
:::
2024-05-04 14:20:58 +00:00
:::warning
The query can be executed in any order and even in a different order each time, so the result of this function is indeterminate.
2024-05-04 14:20:58 +00:00
To get a determinate result, you can use the [`min`](../reference/min.md) or [`max`](../reference/max.md) function instead of `any`.
:::
**Implementation details**
2024-05-04 14:20:58 +00:00
In some cases, you can rely on the order of execution. This applies to cases when `SELECT` comes from a subquery that uses `ORDER BY`.
When a `SELECT` query has the `GROUP BY` clause or at least one aggregate function, ClickHouse (in contrast to MySQL) requires that all expressions in the `SELECT`, `HAVING`, and `ORDER BY` clauses be calculated from keys or from aggregate functions. In other words, each column selected from the table must be used either in keys or inside aggregate functions. To get behavior like in MySQL, you can put the other columns in the `any` aggregate function.
2023-07-16 05:23:15 +00:00
2024-05-04 14:20:58 +00:00
**Example**
Query:
```sql
CREATE TABLE any_nulls (city Nullable(String)) ENGINE=Log;
INSERT INTO any_nulls (city) VALUES (NULL), ('Amsterdam'), ('New York'), ('Tokyo'), ('Valencia'), (NULL);
SELECT any(city), anyRespectNulls(city) FROM any_nulls;
2024-05-04 14:20:58 +00:00
```
```response
┌─any(city)─┬─anyRespectNulls(city)─┐
│ Amsterdam │ ᴺᵁᴸᴸ │
└───────────┴───────────────────────┘
2024-05-04 14:20:58 +00:00
```