Using a parameter for the function to call

Viewed 29

Within PostgresSQL how can I define a parameter as a function, i.e.

SELECT MAX(value) FROM table;
SELECT MIN(value) FROM table;
SELECT AVG(value) FROM table;

where the function is a parameter, something like the following:

SELECT $1(value) FROM table;
1 Answers

You cannot do that. Use multiple separate queries, and choose the one to execute. If you have to do this within SQL, you can also do the choosing depending on a parameter:

SELECT (CASE $1::text
  WHEN 'max' THEN (SELECT MAX(value) FROM table)
  WHEN 'min' THEN (SELECT MIN(value) FROM table)
  WHEN 'avg' THEN (SELECT AVG(value) FROM table)
END);
Related