If you’re getting an error that reads “function mode() does not exist” in PostgreSQL, it could be that you’re using the wrong syntax with this function.
The mode()
function requires a WITHIN GROUP
clause, and we can get the above error if we remove that clause.
In this case, we can fix the error by adding a valid WITHIN GROUP
clause.
Example of Error
Here’s an example of code that produces the error:
SELECT
mode()
FROM products;
Result:
function mode() does not exist
We’re told that the mode()
function doesn’t exist, but we know that it does. However, it doesn’t exist with that syntax (i.e. without a WITHIN GROUP
clause).
Solution
We can fix the above error by adding a valid WITHIN GROUP
clause:
SELECT
mode() WITHIN GROUP ( ORDER BY vendorid )
FROM products;
Result:
mode ------ 1001
This time it ran without error.
Note that we must provide an ORDER BY
clause in the WITHIN GROUP
clause. Omitting the ORDER BY
clause will result in another error.