> ## 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.

> 计算与最大 `val` 值对应的 `arg` 值。

# argMax

<div id="argMax">
  ## argMax
</div>

引入版本：v1.1.0

计算最大 `val` 值对应的 `arg` 值。如果有多行的 `val` 同为最大值，则返回哪个关联的 `arg` 是不确定的。
`arg` 和 `max` 这两部分都作为[聚合函数](/zh/reference/functions/aggregate-functions)处理：它们在处理过程中都会[跳过 `Null`](/zh/reference/functions/aggregate-functions#null-processing)，如果存在非 `Null` 值，则返回非 `Null` 值。

**另请参见**

* [Tuple](/zh/reference/data-types/tuple)

**语法**

```sql theme={null}
argMax(arg, val)
```

**参数**

* `arg` — 要查找其对应最大值的参数。[`const String`](/zh/reference/data-types/string)
* `val` — 用于求最大值的值。[`(U)Int8/16/32/64`](/zh/reference/data-types/int-uint) 或 [`Float*`](/zh/reference/data-types/float) 或 [`Date`](/zh/reference/data-types/date) 或 [`DateTime`](/zh/reference/data-types/datetime) 或 [`Tuple`](/zh/reference/data-types/tuple)

**返回值**

返回与最大 `val` 值对应的 `arg` 值。返回类型与 `arg` 的类型一致。

**示例**

**基本用法**

```sql title=Query theme={null}
SELECT argMax(user, salary) FROM salary;
```

```response title=Response theme={null}
┌─argMax(user, salary)─┐
│ director             │
└──────────────────────┘
```

**带有 NULL 处理的扩展示例**

```sql title=Query theme={null}
CREATE TABLE test
(
    a Nullable(String),
    b Nullable(Int64)
)
ENGINE = Memory AS
SELECT *
FROM VALUES(('a', 1), ('b', 2), ('c', 2), (NULL, 3), (NULL, NULL), ('d', NULL));

SELECT argMax(a, b), max(b) FROM test;
```

```response title=Response theme={null}
┌─argMax(a, b)─┬─max(b)─┐
│ b            │      3 │
└──────────────┴────────┘
```

**在参数中使用 Tuple**

```sql title=Query theme={null}
SELECT argMax(a, (b,a)) FROM test;
```

```response title=Response theme={null}
┌─argMax(a, tuple(b, a))─┐
│ c                      │
└────────────────────────┘
```
