跳转到主内容
跳转到主内容

max

max

引入版本:v1.1

用于计算一组值中最大值的聚合函数。

语法

max(column)

参数

  • column — 列名或表达式。Any

返回值

组内的最大值,类型与输入相同。Any

示例

简单 max 示例

CREATE TABLE employees (name String, salary UInt32) ENGINE = Memory;
INSERT INTO employees VALUES ('Alice', 3000), ('Bob', 4000), ('Charlie', 3500);

SELECT max(salary) FROM employees;
┌─max(salary)─┐
│        4000 │
└─────────────┘

配合 GROUP BY 使用 MAX

CREATE TABLE sales (department String, revenue UInt32) ENGINE = Memory;
INSERT INTO sales VALUES ('Engineering', 100000), ('Engineering', 120000), ('Marketing', 80000), ('Marketing', 90000);

SELECT department, max(revenue) FROM sales GROUP BY department ORDER BY department;
┌─department──┬─max(revenue)─┐
│ Engineering │       120000 │
│ Marketing   │        90000 │
└─────────────┴──────────────┘

关于非聚合最大值的注意事项

-- If you need non-aggregate function to choose a maximum of two values, see greatest():
SELECT greatest(a, b) FROM table;