ウィンドウ内の列の最大値またはexprの最大値を計算します。
制限事項
ウィンドウ関数を使用する前に、次の制限に注意してください。
Window関数は
SELECT
文でのみサポートされます。ウィンドウ関数には、ネストされたウィンドウ関数またはネストされた集計関数を含めることはできません。
同じレベルの集計関数と一緒にウィンドウ関数を使用することはできません。
構文
-- Calculate the maximum value of a column.
max(<colname>)
-- Calculate the maximum value of expr in a window.
max(<expr>) over([partition_clause] [orderby_clause] [frame_clause])
説明
ウィンドウ内の列の最大値またはexprの最大値を計算します。
パラメーター
colname: 必須です。 列の名前。BOOLEAN以外の任意のデータ型にすることができます。
expr: 必須です。 最大値の計算に使用される式。 入力値は、BOOLEAN以外の任意のデータ型とすることができる。 行の入力値がNULLの場合、この行は計算に使用されません。
partition_clause、orderby_clause、およびframe_clause: これらのパラメーターの詳細については、「windowing_definition」をご参照ください。
戻り値
戻り値のデータ型は、colnameパラメーターのデータ型と同じです。 戻り値は、次のルールによって異なります。
colnameの値がnullの場合、この値を含む行は計算に使用されません。
colnameの値がBOOLEAN型の場合、その値は計算には使用されません。
exprと同じ型の値が返されます。
サンプルデータ
このセクションでは、関数の使用方法を理解するためのサンプルソースデータと例を示します。 empという名前のテーブルを作成し、サンプルデータをテーブルに挿入します。 例:
create table if not exists emp
(empno bigint,
ename string,
job string,
mgr bigint,
hiredate datetime,
sal bigint,
comm bigint,
deptno bigint);
tunnel upload emp.txt emp; -- Replace emp.txt with the actual path (path and name) to which you upload the data file.
emp.txtファイルには、次のサンプルデータが含まれています。
7369,SMITH,CLERK,7902,1980-12-17 00:00:00,800,,20
7499,ALLEN,SALESMAN,7698,1981-02-20 00:00:00,1600,300,30
7521,WARD,SALESMAN,7698,1981-02-22 00:00:00,1250,500,30
7566,JONES,MANAGER,7839,1981-04-02 00:00:00,2975,,20
7654,MARTIN,SALESMAN,7698,1981-09-28 00:00:00,1250,1400,30
7698,BLAKE,MANAGER,7839,1981-05-01 00:00:00,2850,,30
7782,CLARK,MANAGER,7839,1981-06-09 00:00:00,2450,,10
7788,SCOTT,ANALYST,7566,1987-04-19 00:00:00,3000,,20
7839,KING,PRESIDENT,,1981-11-17 00:00:00,5000,,10
7844,TURNER,SALESMAN,7698,1981-09-08 00:00:00,1500,0,30
7876,ADAMS,CLERK,7788,1987-05-23 00:00:00,1100,,20
7900,JAMES,CLERK,7698,1981-12-03 00:00:00,950,,30
7902,FORD,ANALYST,7566,1981-12-03 00:00:00,3000,,20
7934,MILLER,CLERK,7782,1982-01-23 00:00:00,1300,,10
7948,JACCKA,CLERK,7782,1981-04-12 00:00:00,5000,,10
7956,WELAN,CLERK,7649,1982-07-20 00:00:00,2450,,10
7956,TEBAGE,CLERK,7748,1982-12-30 00:00:00,1300,,10
例
例1: deptno列を使用してウィンドウを定義し、sal列の最大値を取得します。 ORDER BY句は指定されていません。 この関数は、現在のウィンドウの最大値を返します。 現在のウィンドウには、同じdeptno値を持つ行が含まれています。 例:
select deptno, sal, max(sal) over (partition by deptno) from emp;
次の応答が返されます。
+------------+------------+------------+ | deptno | sal | _c2 | +------------+------------+------------+ | 10 | 1300 | 5000 | -- This row is the first row of this window. The return value is the maximum value among the values from the first row to the sixth row. | 10 | 2450 | 5000 | -- The return value is the maximum value among the values from the first row to the sixth row. | 10 | 5000 | 5000 | -- The return value is the maximum value among the values from the first row to the sixth row. | 10 | 1300 | 5000 | | 10 | 5000 | 5000 | | 10 | 2450 | 5000 | | 20 | 3000 | 3000 | | 20 | 3000 | 3000 | | 20 | 800 | 3000 | | 20 | 1100 | 3000 | | 20 | 2975 | 3000 | | 30 | 1500 | 2850 | | 30 | 950 | 2850 | | 30 | 1600 | 2850 | | 30 | 1250 | 2850 | | 30 | 1250 | 2850 | | 30 | 2850 | 2850 | +------------+------------+------------+
例2: deptno列を使用してウィンドウを定義し、sal列の最大値を取得します。 ORDER BY句が指定されています。 この関数は、現在のウィンドウの最初の行から現在の行までの値のうち、最大値を返します。 現在のウィンドウには、同じdeptno値を持つ行が含まれています。 例:
select deptno, sal, max(sal) over (partition by deptno order by sal) from emp;
次の応答が返されます。
+------------+------------+------------+ | deptno | sal | _c2 | +------------+------------+------------+ | 10 | 1300 | 1300 | -- This row is the first row of this window. | 10 | 1300 | 1300 | -- The return value is the maximum value among the values in the first and second rows. | 10 | 2450 | 2450 | -- The return value is the maximum value among the values from the first row to the third row. | 10 | 2450 | 2450 | -- The return value is the maximum value among the values from the first row to the fourth row. | 10 | 5000 | 5000 | | 10 | 5000 | 5000 | | 20 | 800 | 800 | | 20 | 1100 | 1100 | | 20 | 2975 | 2975 | | 20 | 3000 | 3000 | | 20 | 3000 | 3000 | | 30 | 950 | 950 | | 30 | 1250 | 1250 | | 30 | 1250 | 1250 | | 30 | 1500 | 1500 | | 30 | 1600 | 1600 | | 30 | 2850 | 2850 | +------------+------------+------------+
例3: すべての従業員の最高給与 (sal) を計算します。 例:
select max(sal) from emp;
次の応答が返されます。
+------------+ | _c0 | +------------+ | 5000 | +------------+
例4:
GROUP BY
でこの関数を使用して、すべての従業員を部門別にグループ化し (deptno) 、各部門の従業員の最高給与 (sal) を計算します。 例:select deptno, max(sal) from emp group by deptno;
次の応答が返されます。
+------------+------------+ | deptno | _c1 | +------------+------------+ | 10 | 5000 | | 20 | 3000 | | 30 | 2850 | +------------+------------+
関連関数
MAXは、集約関数または窓関数である。