The GREATEST function compares a group of values and returns the maximum value.
Syntax
greatest(<var1>, <var2>[,...])Parameters
var: required. Input values of the BIGINT, DOUBLE, DECIMAL, DATETIME, DATE, or STRING type.
Return value
Returns the maximum value among the input parameters.
When the input parameters are of the same data type, the return value is of the same data type as the input parameters.
When the input parameters are of different data types, implicit conversions are performed based on the following rules:
Comparisons among DOUBLE, BIGINT, and STRING types are converted to the DOUBLE type.
Comparisons among DECIMAL, DOUBLE, BIGINT, and STRING types are converted to the DECIMAL type.
Comparisons between STRING and DATETIME types are converted to the DATETIME type.
NoteWhen the input format is a STRING type in the format of
yyyy-mm-dd hh:mi:ssand the data type version of the MaxCompute project is 1.0, the input is implicitly converted to the DATETIME type before computation. For more information about data type version settings, see Data type edition 1.0.
NULL values are treated as the minimum values by default. When
set odps.sql.hive.compatible=true;is set and any input parameter is NULL, NULL is returned.
Examples
-- Returns 9.9.
SELECT greatest(2.5,5,3.14BD,'9.9');
-- Returns 2025-10-01 12:00:00.
SELECT greatest(datetime'2025-10-01 12:00:00',null) AS result;
-- Returns 2026-01-01.
SELECT greatest(date'2025-10-01', date'2025-10-03', date'2026-01-01',date'2025-02-02') AS result;
-- Returns 2025-10-02 12:00:00.
set odps.sql.type.system.odps2=false;
SELECT greatest(datetime'2025-10-01 12:00:00', '2025-10-02 12:00:00') AS result;Related funtions
The GREATEST function is a mathematical function. For more information about functions related to data computation and data transformation, see Mathematical functions.