How to Force a Guaranteed Minimum Value When Selecting a Column in SQL Server

Suppose you’re trying to query column, but you need to set a minimum value to be returned, even if the column contains values that are less than that minimum. For example, you want a minimum value of 50 to be returned, even if the column contains values that are less than 50.

We can use the GREATEST() function to build such a query.

Continue reading

How to Fix the Error “The function ‘FIRST_VALUE’ must have an OVER clause with ORDER BY” in SQL Server

If you’re getting error message 4112 that reads “The function ‘FIRST_VALUE’ must have an OVER clause with ORDER BY” when using the FIRST_VALUE() function, it’s probably because you’re omitting the ORDER BY clause from the OVER clause.

In SQL Server, the FIRST_VALUE() function requires an OVER clause that contains an ORDER BY clause. This error happens when we provide the OVER clause but not the ORDER BY clause.

To fix this error, simply add an ORDER BY clause to the OVER clause.

Continue reading

3 Ways to Fix “A TOP can not be used in the same query or sub-query as a OFFSET” Error in SQL Server

If you’re getting an error that reads “A TOP can not be used in the same query or sub-query as a OFFSET” when running a query in SQL Server, it’s probably because you’re using the TOP() clause and the OFFSET clause in the same query or sub-query.

We can’t use the TOP() clause and the OFFSET clause in the same query in SQL Server.

Below are three options for fixing this error.

Continue reading

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

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

The LAG() 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 LAG() function.

Continue reading

How to Fix the Error: “The function ‘LEAD’ must have an OVER clause with ORDER BY” in SQL Server

If you’re getting an error message that reads “The function ‘LEAD’ must have an OVER clause with ORDER BY” in SQL Server, it’s probably because you’ve omitted the ORDER BY clause from the OVER clause when using the LEAD() function.

The LEAD() function requires an OVER clause that contains an ORDER BY clause. This error happens when we include the OVER clause but not the ORDER BY clause.

To fix this error, add an ORDER BY clause to the OVER clause.

Continue reading

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

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

Window functions such as ROW_NUMBER() require an OVER clause (and that clause must have an ORDER BY clause).

To fix this issue, add an OVER clause when calling the ROW_NUMBER() function.

Continue reading

Fix Error “The function ‘DENSE_RANK’ must have an OVER clause with ORDER BY” in SQL Server

If you’re getting an error that reads “The function ‘DENSE_RANK’ must have an OVER clause with ORDER BY” in SQL Server, it’s probably because you’re calling the DENSE_RANK() function without an ORDER BY clause.

Window functions such as DENSE_RANK() require an OVER clause, and that clause must have an ORDER BY clause. If you’re getting the above error, it’s likely that you’re providing an OVER clause, but you’re omitting the ORDER BY clause.

To fix this error, add an ORDER BY clause to the OVER clause.

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