Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add parameter(s) to an aggregate's value expression in Postgres?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

SELECT max(my_real_column, SELECT $1) FROM my_table

But this similarly does not work.

How can I accomplish this with standard SQL?

>Solution :

max only accepts one value. To calculate the maximum value of max(..) and a fixed value, use the greatest() function and pass the max(..) and the fixed value:

SELECT greatest(max(my_real_column), $1)
...
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading