> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-mintlify-8a08bda2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> É um alias para any, mas foi introduzido para compatibilidade com funções de janela, em que às vezes é necessário processar valores `NULL` (por padrão, todas as funções de agregação do ClickHouse ignoram valores NULL).

# first_value

É um alias para [`any`](/pt-BR/reference/functions/aggregate-functions/any), mas foi introduzido para compatibilidade com [funções de janela](/pt-BR/reference/functions/window-functions), em que às vezes é necessário processar valores `NULL` (por padrão, todas as funções de agregação do ClickHouse ignoram valores NULL).

Ele permite declarar um modificador para respeitar nulos (`RESPECT NULLS`), tanto em [funções de janela](/pt-BR/reference/functions/window-functions) quanto em agregações normais.

Assim como em `any`, sem funções de janela o resultado será aleatório se o fluxo de origem não estiver ordenado, e o tipo de retorno
corresponder ao tipo de entrada (`NULL` só é retornado se a entrada for Nullable ou se o combinador -OrNull for adicionado).

<div id="examples">
  ## exemplos
</div>

```sql theme={null}
CREATE TABLE test_data
(
    a Int64,
    b Nullable(Int64)
)
ENGINE = Memory;

INSERT INTO test_data (a, b) VALUES (1,null), (2,3), (4, 5), (6,null);
```

<div id="example1">
  ### Exemplo 1
</div>

Por padrão, o valor NULL é ignorado.

```sql theme={null}
SELECT first_value(b) FROM test_data;
```

```text theme={null}
┌─any(b)─┐
│      3 │
└────────┘
```

<div id="example2">
  ### Exemplo 2
</div>

O valor NULL é ignorado.

```sql theme={null}
SELECT first_value(b) ignore nulls FROM test_data
```

```text theme={null}
┌─any(b) IGNORE NULLS ─┐
│                    3 │
└──────────────────────┘
```

<div id="example3">
  ### Exemplo 3
</div>

O valor NULL é aceito.

```sql theme={null}
SELECT first_value(b) respect nulls FROM test_data
```

```text theme={null}
┌─any(b) RESPECT NULLS ─┐
│                  ᴺᵁᴸᴸ │
└───────────────────────┘
```

<div id="example4">
  ### Exemplo 4
</div>

Resultado estabilizado com a subconsulta usando `ORDER BY`.

```sql theme={null}
SELECT
    first_value_respect_nulls(b),
    first_value(b)
FROM
(
    SELECT *
    FROM test_data
    ORDER BY a ASC
)
```

```text theme={null}
┌─any_respect_nulls(b)─┬─any(b)─┐
│                 ᴺᵁᴸᴸ │      3 │
└──────────────────────┴────────┘
```
