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

> 计算最大 `val` 对应的 `arg` 和 `val` 值。如果有多行的 `val` 同为最大值，则会返回哪个关联的 `arg` 和 `val` 并不确定。

# argAndMax

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

引入于：v1.1.0

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

<Note>
  与 `argMax` 的唯一区别在于，`argAndMax` 会同时返回参数和值。
</Note>

**另请参见**

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

**语法**

```sql theme={null}
argAndMax(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)

**返回值**

返回一个 Tuple，其中包含与最大 `val` 值对应的 `arg` 值，以及最大 `val` 值。[`Tuple`](/zh/reference/data-types/tuple)

**示例**

**基本用法**

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

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

**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), argAndMax(a, b), max(b) FROM test;
```

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

**在参数中使用 Tuple**

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

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

**另见**

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