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

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

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

To fix this issue, include an OVER clause when calling the LAST_VALUE() function.

Continue reading

Fix “Incorrect syntax near the keyword ‘DISTINCT'” Error in SQL Server

If you’re getting an error that reads “Incorrect syntax near the keyword ‘DISTINCT’” when using the DISTINCT clause in SQL Server, it could be that you’ve put the DISTINCT clause in the wrong position.

When using the DISTINCT clause, it must be the first item in the SELECT list.

Therefore, to fix this error, check the position of the DISTINCT keyword. If it’s not the first item in the SELECT list, move it to the front so that it is the first item in the SELECT list.

Continue reading

How the SQL UNION Operator Deals with NULL Values

The SQL UNION operator concatenates the results of two queries into a single result set. By default it returns distinct rows (i.e. it removes any redundant duplicate rows from the result set). But we can also use UNION ALL to return non-distinct rows (i.e. retain duplicates).

When it comes to NULL values, it’s pretty straight forward. SQL treats two NULL values as non distinct values. In other words, they’re duplicates.

Continue reading

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

When using window functions such as RANK() in SQL Server, we must provide an OVER clause clause with an ORDER BY clause.

If you’re getting an error that reads “The function ‘RANK’ must have an OVER clause with ORDER BY”, it’s probably because you’re including an OVER clause with the RANK() function (as is required), but you’re omitting the ORDER BY clause.

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

Continue reading

Fix Error “function lag(numeric, numeric) does not exist” in PostgreSQL

If you’re getting an error in PostgreSQL that reads something like “function lag(numeric, numeric) does not exist“, it could be because your second argument is the wrong data type.

The second argument to the lag() function is optional, but if it’s provided, it must be an integer.

So to fix this issue, make sure the second argument is an integer. Alternatively, you can omit the second argument altogether if you’re happy to use the default value of 1.

Continue reading

How to Limit the Rows Returned by a SQL Query

When writing SQL queries, we’ll often use a WHERE clause or HAVING clause to narrow the results down to just those rows that we’re interested in.

But sometimes we might want to reduce the number of rows returned without adding extra filtering criteria. Sometimes we might just want to see a handful of rows, without hundreds, thousands or even millions of rows being returned.

Continue reading

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

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

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

Continue reading

Why you’re Getting “The ORDER BY in WITHIN GROUP clause of ‘APPROX_PERCENTILE_DISC’ function must have exactly one expression” in SQL Server

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

The APPROX_PERCENTILE_DISC() function accepts just one ORDER BY expression in its WITHIN GROUP clause.

To fix, be sure to use just one ORDER BY expression in the WITHIN GROUP clause when using the APPROX_PERCENTILE_DISC() function.

Continue reading