I have a query where I need to add a positional argument into a call to max(expression)
.
SELECT max(my_real_column) FROM my_table
I’ve tried the concatenation operator, e.g. my_real_column || $1
, but that yields an operator error:
SELECT max(my_real_column || $1) FROM my_table
The postgres docs show that an aggregate function can take multiple expressions, so I tried this:
SELECT max(my_real_column, SELECT $1) FROM my_table
But this similarly does not work.
How can I accomplish this with standard SQL?
2
Answers
max
only accepts one value. To calculate the maximum value ofmax(..)
and a fixed value, use thegreatest()
function and pass themax(..)
and the fixed value:add a positional argument to
max(expression)
in PostgreSQLuse a
CASE
statement