In SQL Server the STDEV()
function returns the statistical standard deviation of all values in a specified expression.
So we can use this function to get the standard deviation of all values in a column.
Continue readingIn SQL Server the STDEV()
function returns the statistical standard deviation of all values in a specified expression.
So we can use this function to get the standard deviation of all values in a column.
Continue readingWhen 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.
SQL Server has a GROUPING_ID()
function that returns the level of grouping based on its arguments.
We pass one or more column expressions to the function, and it computes the level of grouping. The column expressions must match the ones provided by the GROUP BY
clause.
If you’ve ever used the UNION
clause in a SQL query, chances are you used UNION ALL
or just UNION
by itself. But some RDBMSs also accept a UNION DISTINCT
option.
The UNION DISTINCT
option is basically the equivalent of UNION
by itself. That is, it removes redundant duplicate rows.
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
.
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 readingWhen using the GROUP BY
clause in SQL Server, we can use the GROUPING SETS()
option to combine multiple GROUP BY
clauses into one GROUP BY
clause. When we do this, the results are the equivalent of doing a UNION ALL
of the specified groups.
SQL Server provides GROUP BY CUBE()
functionality that we can use in our SQL queries, which creates groups for all possible combinations of columns.
It’s similar to the GROUP BY ROLLUP()
functionality, except that it can provide us with more information, due to the fact that it groups all possible combinations.
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.
When we use the GROUP BY
clause in SQL Server, we have several options for specifying how the GROUP BY
operation is applied. One such option is the ROLLUP
modifier. We can use this modifier to create subtotals and grand totals.