メインコンテンツへスキップ
メインコンテンツへスキップ

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 での最大値

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 │
└─────────────┴──────────────┘

非集約 max に関する注意

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