すべてのプロダクト
Search
ドキュメントセンター

MaxCompute:STDDEV

最終更新日:Jan 17, 2025

すべての入力値の母集団標準偏差を計算します。

制限事項

ウィンドウ関数を使用する前に、次の制限に注意してください。

  • Window関数はSELECT文でのみサポートされます。

  • ウィンドウ関数には、ネストされたウィンドウ関数またはネストされた集計関数を含めることはできません。

  • 同じレベルの集計関数と一緒にウィンドウ関数を使用することはできません。

使用上の注意

MaxCompute V2.0は追加機能を提供します。 使用する関数に新しいデータ型が含まれる場合は、MaxCompute V2.0データ型エディションを有効にする必要があります。 新しいデータ型には、TINYINT、SMALLINT、INT、FLOAT、VARCHAR、TIMESTAMP、およびBINARYが含まれます。

  • セッションレベル: MaxCompute V2.0データ型エディションを使用するには、実行するsql文の前にset odps. SQL. type.system.odps2=true; を追加し、一緒にコミットして実行する必要があります。

  • プロジェクトレベル: プロジェクトの所有者は、プロジェクトの要件に基づいて、プロジェクトのMaxCompute V2.0データ型エディションを有効にできます。 設定は10〜15分後に有効になります。 サンプルコマンド:

    setproject odps.sql.type.system.odps2=true;

    setprojectの詳細については、「プロジェクト操作」をご参照ください。 プロジェクトレベルでMaxCompute V2.0データ型エディションを有効にする際の注意事項の詳細については、「データ型エディション」をご参照ください。

複数の集計関数を含むSQL文を使用していて、プロジェクトのリソースが不足している場合、メモリオーバーフローが発生する可能性があります。 ビジネス要件に基づいて、SQLステートメントを最適化するか、コンピューティングリソースを購入することを推奨します。

構文

-- Calculate the population standard deviation of all the values in a column.
double stddev(double <colname>)
decimal stddev(decimal <colname>)

-- Calculate the population standard deviation of expr in a window.
double stddev|stddev_pop([distinct] <expr>) over ([partition_clause] [orderby_clause] [frame_clause])
decimal stddev|stddev_pop([distinct] <expr>) over ([partition_clause] [orderby_clause] [frame_clause])

パラメーター

  • colname: 必須です。 列の名前。DOUBLE型またはDECIMAL型にすることができます。 指定された列がSTRING型またはBIGINT型の場合、列の値は計算前に暗黙的にDOUBLE型に変換されます。

  • expr: 必須です。 母集団の標準偏差を計算するために使用される式。 入力値は、DOUBLEまたはDECIMALタイプにすることができます。

    • 入力値がSTRING型またはBIGINT型の場合、計算前に暗黙的にDOUBLE型の値に変換されます。 別のデータ型の場合は、エラーが返されます。

    • 行の値がnullの場合、この行は計算に使用されません。

    • distinctキーワードが指定されている場合、distinct値の母集団標準偏差が計算されます。

  • partition_clauseorderby_clause、およびframe_clause: これらのパラメーターの詳細については、「windowing_definition」をご参照ください。

戻り値

  • 行のcolnameで指定した列の値がnullの場合、その行は計算に使用されません。 入力データと戻り値のデータ型の対応関係を次の表に示します。

    入力タイプ

    戻り値タイプ

    TINYINT

    DOUBLE

    SMALLINT

    DOUBLE

    INT

    DOUBLE

    BIGINT

    DOUBLE

    FLOAT

    DOUBLE

    DOUBLE

    DOUBLE

    DECIMAL

    DECIMAL

  • exprと同じデータ型の値が返されます。 exprで指定されたすべての式の値がnullの場合、nullが返されます。

サンプルデータ

このセクションでは、関数の使用方法を理解するためのサンプルソースデータと例を示します。 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: sal列のすべての値の母集団標準偏差を計算します。 例:

    select stddev(sal) from emp;

    次の応答が返されます。

    +------------+
    | _c0        |
    +------------+
    | 1262.7549932628976 |
    +------------+
  • 例2: GROUP BYでこの関数を使用して、すべての従業員を部門 (deptno) ごとにグループ化し、各部門の従業員の給与値の人口標準偏差を計算します。 例:

    select deptno, stddev(sal) from emp group by deptno;

    次の応答が返されます。

    +------------+------------+
    | deptno     | _c1        |
    +------------+------------+
    | 10         | 1546.1421524412158 |
    | 20         | 1004.7387720198718 |
    | 30         | 610.1001739241043 |
    +------------+------------+
  • 例3: deptno列を使用してウィンドウを定義し、sal列の母集団標準偏差を計算します。 ORDER BY句は指定されていません。 この関数は、現在のウィンドウの累積母集団標準偏差を返します。 現在のウィンドウには、同じdeptno値を持つ行が含まれています。 例:

    select deptno, sal, stddev(sal) over (partition by deptno) from emp;

    次の応答が返されます。

    +------------+------------+------------+
    | deptno     | sal        | _c2        |
    +------------+------------+------------+
    | 10         | 1300       | 1546.1421524412158 |   -- This row is the first row of this window. The return value is the cumulative population standard deviation of the values from the first row to the sixth row. 
    | 10         | 2450       | 1546.1421524412158 |   -- The return value is the cumulative population standard deviation of the values from the first row to the sixth row. 
    | 10         | 5000       | 1546.1421524412158 |
    | 10         | 1300       | 1546.1421524412158 |
    | 10         | 5000       | 1546.1421524412158 |
    | 10         | 2450       | 1546.1421524412158 |
    | 20         | 3000       | 1004.7387720198718 |
    | 20         | 3000       | 1004.7387720198718 |
    | 20         | 800        | 1004.7387720198718 |
    | 20         | 1100       | 1004.7387720198718 |
    | 20         | 2975       | 1004.7387720198718 |
    | 30         | 1500       | 610.1001739241042 |
    | 30         | 950        | 610.1001739241042 |
    | 30         | 1600       | 610.1001739241042 |
    | 30         | 1250       | 610.1001739241042 |
    | 30         | 1250       | 610.1001739241042 |
    | 30         | 2850       | 610.1001739241042 |
    +------------+------------+------------+
  • 例4: 非Hive互換モードでは、deptno列を使用してウィンドウを定義し、sal列の母集団標準偏差を計算します。 ORDER BY句が指定されています。 この関数は、現在のウィンドウの最初の行から現在の行までの値の累積母集団標準偏差を返します。 現在のウィンドウには、同じdeptno値を持つ行が含まれています。 例:

    -- Disable the Hive-compatible mode. 
    set odps.sql.hive.compatible=false;
    -- Execute the following statement: 
    select deptno, sal, stddev(sal) over (partition by deptno order by sal) from emp;

    次の応答が返されます。

    +------------+------------+------------+
    | deptno     | sal        | _c2        |
    +------------+------------+------------+
    | 10         | 1300       | 0.0        |           -- This row is the first row of this window. 
    | 10         | 1300       | 0.0        |           -- The return value is the cumulative population standard deviation of the values in the first and second rows. 
    | 10         | 2450       | 542.1151989096865 |    -- The return value is the cumulative population standard deviation of the values from the first row to the third row. 
    | 10         | 2450       | 575.0      |           -- The return value is the cumulative population standard deviation of the values from the first row to the fourth row. 
    | 10         | 5000       | 1351.6656391282572 |
    | 10         | 5000       | 1546.1421524412158 |
    | 20         | 800        | 0.0        |
    | 20         | 1100       | 150.0      |
    | 20         | 2975       | 962.4188277460079 |
    | 20         | 3000       | 1024.2947268730811 |
    | 20         | 3000       | 1004.7387720198718 |
    | 30         | 950        | 0.0        |
    | 30         | 1250       | 150.0      |
    | 30         | 1250       | 141.4213562373095 |
    | 30         | 1500       | 194.8557158514987 |
    | 30         | 1600       | 226.71568097509268 |
    | 30         | 2850       | 610.1001739241042 |
    +------------+------------+------------+
  • 例5: Hive互換モードで、deptno列を使用してウィンドウを定義し、sal列の母集団標準偏差を計算します。 ORDER BY句が指定されています。 この関数は、最初の行から現在のウィンドウの現在の行と同じsal値を持つ行までの値の累積母集団標準偏差を返します。 同じsal値を持つ行の母集団の標準偏差は同じです。 現在のウィンドウには、同じdeptno値を持つ行が含まれています。 サンプル文:

    -- Enable the Hive-compatible mode. 
    set odps.sql.hive.compatible=true;
    -- Execute the following statement: 
    select deptno, sal, stddev(sal) over (partition by deptno order by sal) from emp;

    次の応答が返されます。

    +------------+------------+------------+
    | deptno     | sal        | _c2        |
    +------------+------------+------------+
    | 10         | 1300       | 0.0        |           -- This row is the first row of this window. The population standard deviation for the first row is the cumulative population standard deviation of the values in the first and second rows because the two rows have the same sal value. 
    | 10         | 1300       | 0.0        |           -- The return value is the cumulative population standard deviation of the values in the first and second rows. 
    | 10         | 2450       | 575.0      |           -- The population standard deviation for the third row is the cumulative population standard deviation of the values from the first row to the fourth row because the third and fourth rows have the same sal value. 
    | 10         | 2450       | 575.0      |           -- The return value is the cumulative population standard deviation of the values from the first row to the fourth row. 
    | 10         | 5000       | 1546.1421524412158 |
    | 10         | 5000       | 1546.1421524412158 |
    | 20         | 800        | 0.0        |
    | 20         | 1100       | 150.0      |
    | 20         | 2975       | 962.4188277460079 |
    | 20         | 3000       | 1004.7387720198718 |
    | 20         | 3000       | 1004.7387720198718 |
    | 30         | 950        | 0.0        |
    | 30         | 1250       | 141.4213562373095 |
    | 30         | 1250       | 141.4213562373095 |
    | 30         | 1500       | 194.8557158514987 |
    | 30         | 1600       | 226.71568097509268 |
    | 30         | 2850       | 610.1001739241042 |
    +------------+------------+------------+

関連関数

STDDEVは、集約関数またはウィンドウ関数である。

  • 複数の入力レコードの平均値と集計パラメーターの計算に使用される関数の詳細については、集計関数.

  • ウィンドウ内の列のデータの合計を計算し、データをソートする関数の詳細については、ウィンドウ関数.