Fixing Error 10754: “The function ‘APPROX_PERCENTILE_DISC’ must have a WITHIN GROUP clause” in SQL Server

When using the APPROX_PERCENTILE_DISC() function in SQL Server, you may get an error that reads “The function ‘APPROX_PERCENTILE_DISC’ must have a WITHIN GROUP clause“. If you get this error, it’s because you omitted the WITHIN GROUP clause when using the APPROX_PERCENTILE_DISC() function.

To fix this issue, make sure you include the WITHIN GROUP clause whenever you use the APPROX_PERCENTILE_DISC() function.

Continue reading

An Introduction to the IS [NOT] DISTINCT FROM Predicate in SQL Server

SQL Server 2022 introduced the IS [NOT] DISTINCT FROM predicate that compares the equality of two expressions and guarantees a true or false result, even if one or both operands are NULL.

Normally if we compare two NULL values, they will always be different (although this will depend on your ANSI_NULLS setting – setting ANSI_NULLS to OFF will result in NULLs being treated as equal). The IS [NOT] DISTINCT FROM predicate enables us to compare NULLs as though they’re equal, even when our ANSI_NULLS setting is set to ON.

Continue reading

CREATE TABLE IF NOT EXISTS Equivalent in SQL Server

In SQL, we can use the CREATE TABLE IF NOT EXISTS statement to create a table only if it doesn’t exist. The benefit of doing this is that we won’t get an error if there’s already a table with the same name.

But SQL Server doesn’t support this syntax – at least not in the current version of SQL Server at the time of writing (SQL Server 2022) .

So with SQL Server, we need to do a bit of extra work.

Continue reading

Fix “Argument value 0 is invalid for argument 3 of generate_series function” in SQL Server

If you’re getting SQL Server error 4199 that reads “Argument value 0 is invalid for argument 3 of generate_series function“, it’s probably because you’re passing zero as the third argument to the GENERATE_SERIES() function.

The GENERATE_SERIES() function accepts an optional third argument, but this argument can’t be zero.

To fix this error, either pass a non-zero expression as the third argument, or omit the argument altogether (in order to use the default step of 1).

Continue reading

Generate a Time Series in SQL Server

SQL Server’s GENERATE_SERIES() function returns a series of numbers within a given interval. But just because it returns numbers, doesn’t mean we can’t leverage its functionality in order to generate a series of time values.

If you need to create a series of time values with evenly spaced intervals between a start and end point, maybe the following technique can help.

Continue reading

Fix “The ORDER BY in WITHIN GROUP clause of ‘APPROX_PERCENTILE_CONT’ function must have exactly one expression” in SQL Server

If you’re getting SQL Server error 10751 that reads “The ORDER BY in WITHIN GROUP clause of ‘APPROX_PERCENTILE_CONT’ function must have exactly one expression” it’s probably because you’re using too many ORDER BY expressions with the APPROX_PERCENTILE_CONT() function.

The APPROX_PERCENTILE_CONT() function requires the WITHIN GROUP clause, and that clause requires an ORDER BY sub-clause. However, that ORDER BY sub-clause requires exactly one expression – no more, no less. So, you can’t pass multiple expressions, and you can’t pass zero expressions. It must be exactly one expression.

To fix this issue, be sure to have just one ORDER BY expression in the WITHIN GROUP clause when using the APPROX_PERCENTILE_CONT() function.

Continue reading

Fixing the Error: “The function ‘LAG’ must have an OVER clause with ORDER BY” in SQL Server

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

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