跳转到主要内容

max

引入版本:v1.1.0 用于计算一组值中的最大值的聚合函数。 语法
max(column)
参数
  • column — 列名或表达式。Any
返回值 组内的最大值,返回类型与输入值的类型相同。Any 示例 简单的 max 示例
Query
CREATE TABLE employees (name String, salary UInt32) ENGINE = Memory;
INSERT INTO employees VALUES ('Alice', 3000), ('Bob', 4000), ('Charlie', 3500);

SELECT max(salary) FROM employees;
Response
┌─max(salary)─┐
│        4000 │
└─────────────┘
结合 GROUP BY 使用 max
Query
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;
Response
┌─department──┬─max(revenue)─┐
│ Engineering │       120000 │
│ Marketing   │        90000 │
└─────────────┴──────────────┘
非聚合最大值说明
Query
-- 如果需要用非聚合函数取两个值中的较大值,请参阅 greatest():
SELECT greatest(a, b) FROM table;
Response
最后修改于 2026年6月10日