GREATEST(expr1, expr2, ...) returns the largest of its arguments and skips nulls, so a row with one missing value still gets an answer. DuckDB's greatest() skips nulls the same way. Snowflake's and BigQuery's GREATEST return null if any argument is null: use GREATEST_IGNORE_NULLS in Snowflake and coalesce the arguments in BigQuery. For the null-propagating version in Tableau, see GREATESTPROPAGATENULL.
Updated · Checked on the xmetric engine
What is the syntax?
GREATEST(expr1, expr2, ...)
expr1, expr2, ...
two or more numbers or numeric expressions.
Returns the largest of the non-null arguments. Null only if every argument is null.
What does it return, and what is the SQL?
Each example runs on the same 8 sample rows twice: the Tableau formula on the xmetric engine, and the SQL in DuckDB.
Which is larger, Value or Other?
Tableau
GREATEST([Value], [Other])
SQL (DuckDB)
SELECTgreatest(Value, Other) AS result
FROM input
ORDERBY id
Row 3 has no Other, so it returns the value, 0.125; row 6 has no Value, so it returns Other, 2.
Value
Other
Result
2.5
4
4
−2.5
−3
−2.5
0.13
0.13
−0.4
−1
−0.4
3.75
8
8
2
2
0
0.5
0.5
16
16
16
✓ The Tableau formula and the SQL return the same 8 rows.
How do I clamp values at zero?
Tableau
GREATEST([Value], 0)
SQL (DuckDB)
SELECTgreatest(Value, 0) AS result
FROM input
ORDERBY id
Negatives become 0. Row 6 has no value, and the result is 0, the one argument left.
Value
Result
2.5
2.5
−2.5
0
0.13
0.13
−0.4
0
3.75
3.75
0
0
0
16
16
✓ The Tableau formula and the SQL return the same 8 rows.
Where does a straight translation go wrong?
Common mistake
Why does a hand-written CASE lose the value when one side is null?
CASE WHEN Value > Other THEN Value ELSE Other END compares with a null on row 3, the test is unknown, and it falls through to Other, which is null. GREATEST returns the value that is there, 0.125.
Tableau
GREATEST([Value], [Other])
Tempting SQL
SELECTCASEWHEN Value > Other THEN Value ELSE Other ENDAS result
FROM input
ORDERBY id
SQL that matches
SELECTgreatest(Value, Other) AS result
FROM input
ORDERBY id
Value
Other
Tableau
Tempting SQL
SQL that matches
2.5
4
4
4
4
−2.5
−3
−2.5
−2.5
−2.5
0.13
0.13
null
0.13
−0.4
−1
−0.4
−0.4
−0.4
3.75
8
8
8
8
2
2
2
2
0
0.5
0.5
0.5
0.5
16
16
16
16
16
✓ The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.
How do I write it in Snowflake or BigQuery?
The first example, written for each warehouse from its documentation. These are not run here; DuckDB is.
Snowflake
SELECTGREATEST_IGNORE_NULLS(Value, Other) AS result
FROM input
ORDERBY id