2 Possible Reasons You’re Getting “function nth_value(numeric) does not exist” in PostgreSQL

There are at least a couple of reasons you might get an error that reads “function nth_value(numeric) does not exist” in PostgreSQL.

It could be that you’re calling the nth_value() function without the correct number of arguments. It could also be that you’re passing the wrong argument type.

To fix this issue, be sure to pass the correct number of arguments, with the correct type.

Continue reading

Fix Error “invalid input syntax for type integer” When using the NTILE() Function in PostgreSQL

If you’re getting an error that reads “ERROR:  invalid input syntax for type integer” in PostgreSQL, it’s probably because you’re calling the ntile() function with an argument of the wrong data type.

This error can occur when we pass the wrong type to the ntile() function. We can get a different error depending on the type, but this error can occur when we pass a string.

To fix this issue, be sure to pass a positive integer to the function.

Continue reading

Understanding the PERCENTILE_DISC() Function in SQL Server

In SQL Server, PERCENTILE_DISC() is a window function that returns a percentile value based on a discrete distribution of the input column. Basically, it returns the first value in the set whose ordered position is the same or more than the specified fraction.

The output of PERCENTILE_DISC() is equal to a specific column value (unlike the PERCENTILE_CONT() function, which could calculate a value that isn’t in the column).

When we call PERCENTILE_DISC() we specify the percentile to use. It then performs its calculation based on that percentile.

Continue reading

Fix Error “The function ‘PERCENT_RANK’ must have an OVER clause” in SQL Server

In SQL Server, if you’re getting an error that reads “The function ‘PERCENT_RANK’ must have an OVER clause”, it’s because you’re calling the PERCENT_RANK() function without an OVER clause.

The PERCENT_RANK() function requires an OVER clause (and that clause must have an ORDER BY clause).

To fix this issue, be sure to include an OVER clause when calling the PERCENT_RANK() function.

Continue reading

Create a Window Function in SQL

Most of the major RDBMSs allow us to create window functions in SQL queries.

A window function is a special aggregate-like function that enables partitioning and ordering of data within a result set. Unlike regular aggregate functions, which collapse multiple rows into a single result, window functions retain individual rows in the output, augmenting them with additional computed values.

The following example demonstrates how to create a window function in SQL.

Continue reading