max
max
값 그룹에서 최댓값을 계산하는 집계 함수예요. v1.1.0부터 도입되었어요.
출처: 문서
본문
값 그룹에서 최댓값을 계산하는 집계 함수예요.
구문 (Syntax)
max(column)
인자 (Arguments)
column— 컬럼 이름 또는 표현식이에요.Any
반환 값 (Returned value)
그룹 전체의 최댓값으로, 입력과 같은 타입이에요. Any
예시 (Examples)
간단한 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 │
└─────────────┴──────────────┘
비집계(비그룹) 최댓값에 대한 참고:
-- 두 값 중 더 큰 값을 고르는 비집계 함수가 필요하다면 greatest()를 참고하세요:
SELECT greatest(a, b) FROM values('a Int32, b Int32', (1, 2), (5, 3));
결과:
┌─greatest(a, b)─┐
│ 2 │
│ 5 │
└────────────────┘