If you have a query that returns multiple columns with numeric values in SQL Server, you can pass them to the GREATEST()
function to find out which column has the highest value.
Tag: how to
5 Ways to Calculate the Difference Between Values in the Same Column (But Different Rows) in MySQL
MySQL has a bunch of functions that enable us to get values from another row in the same column. This makes it easy for us to do stuff like calculate the difference between a value in the current row and one in another row, even if it’s in the same column.
Here are five options for calculating the difference between a value in the current row and a value in a different row but in the same column.
Continue readingFix SQL Server Error 189: “The greatest function requires 1 to 254 arguments”
If you’re getting SQL Server error number 189 that reads “The greatest function requires 1 to 254 arguments“, it’s probably because you’re either passing too few or too many arguments.
As the error message alludes to, you need to pass at least 1 argument, and no more than 254 arguments when using the GREATEST()
function.
To fix this issue, be sure to pass at least 1 argument, and no more than 254 arguments.
Continue readingFixing the Error: “The function ‘LAST_VALUE’ must have an OVER clause with ORDER BY” in SQL Server
If you’re getting an error message that reads “The function ‘LAST_VALUE’ must have an OVER clause with ORDER BY.” when using the LAST_VALUE()
function in SQL Server, it’s probably because you’ve omitted the ORDER BY
clause from the OVER
clause.
The LAST_VALUE()
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.
3 Ways to Remove Duplicate Rows from Query Results in SQL
Sometimes when we run a SQL query, we only want to see unique rows. But depending on the columns we’re selecting, we might end up with duplicate rows. And this could happen without even knowing it, especially with large data sets.
But it doesn’t have to be this way.
Fortunately most SQL databases provide us with an easy way to remove duplicates.
Continue readingFixing the “data types” are “incompatible in the approx_percentile_cont operator” in SQL Server
If you’re getting SQL Server error msg 402 that tells you the “data types” are “incompatible in the approx_percentile_cont operator“, it’s probably because you’re trying to apply the APPROX_PERCENTILE_CONT()
function against a non-numeric column (or one that doesn’t evaluate to a numeric type).
When using the APPROX_PERCENTILE_CONT()
function, the ORDER BY
expression in the WITHIN GROUP
clause must evaluate to an exact or approximate numeric type. Other data types are not allowed, and will result in the above error.
To fix this issue, be sure to apply the function against a numeric column/expression.
Continue readingFix 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).
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.
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.
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