Hitachi

Hitachi Advanced Database SQL Reference


7.22.4 MAX

MAX determines the maximum value.

Organization of this subsection

(1) Specification format

general-set-function-MAX ::= {MAX([ALL] value-expression)|MAX(DISTINCT value-expression)}

Note: Whichever form is specified, the results will be the same.

(2) Explanation of specification format

MAX([ALL] value-expression):

Determines the maximum value of the results of the value expression. For details about value expressions, see 7.20 Value expression.

ALL can be omitted. The results will be the same regardless of whether it is specified.

MAX(DISTINCT value-expression):

Determines the maximum value of the results of the value expression. For details about value expressions, see 7.20 Value expression.

(3) Rules

  1. Null values are not included in the calculation.

  2. In the following cases, the execution result will be a null value.

    • If the number of input rows is 0

    • If the values to be calculated are all null values

  3. The following table shows the data type that can be specified in the value expression and the data type of the execution result of the general set function MAX.

    Table 7‒17: Relationship between data type that can be specified in the value expression and data type of the execution result of the general set function MAX

    No.

    Data type that can be specified in the value expression

    Data type of the execution result of general set function MAX

    1

    INTEGER

    INTEGER

    2

    SMALLINT

    SMALLINT

    3

    DECIMAL(m,n)

    DECIMAL(m,n)

    4

    DOUBLE PRECISION

    DOUBLE PRECISION

    5

    CHARACTER(n)

    CHARACTER(n)

    6

    VARCHAR(n)

    VARCHAR(n)

    7

    DATE

    DATE

    8

    TIME(p)

    TIME(p)

    9

    TIMESTAMP(p)

    TIMESTAMP(p)

(4) Examples

Example 1

Using the data in the employee table (EMPLIST), this example determines the age (AGE) of the oldest male employee.

SELECT MAX("AGE") AS "MAX-AGE"
    FROM "EMPLIST"
       WHERE "SEX"='M'

Example of execution results

[Figure]

Example 2

Using the data in the employee table (EMPLIST), this example determines the age (AGE) of the oldest employee in each section (SCODE).

SELECT "SCODE",MAX("AGE") AS "MAX-AGE"
    FROM "EMPLIST"
        GROUP BY "SCODE"

Example of execution results

[Figure]