Fix SQL Server Error: “The function ‘APPROX_PERCENTILE_CONT’ must have a WITHIN GROUP clause”

If you’re getting SQL Server error 10754 that reads “The function ‘APPROX_PERCENTILE_CONT’ must have a WITHIN GROUP clause” it’s probably because you’re calling the APPROX_PERCENTILE_CONT() function, but you’ve omitted the WITHIN GROUP clause.

To fix this issue, add a WITHIN GROUP clause to the function (and make sure it has an ORDER BY clause).

Continue reading

How NULL Values Can Affect your Results When Using the COUNT() Function in SQL

The SQL COUNT() function is a handy tool for telling us how many rows would be returned in a query. We can pass a column name to the function or we can pass the asterisk (*) wildcard to indicate all columns.

If a column contains NULL values, we could get different results, depending on whether we use the column name or the asterisk (*).

Continue reading

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