SQL Server aggregate functions (variance and standard deviation)

Variance and standard deviation can only be used for numeric columns, and the NULLvalue will be ignored.

One, variance and standard deviation

The calculation formula of population variance:
Insert picture description here
In actual tests, the mean of the population is difficult to obtain. It is necessary to use sample statistics instead of population parameters, and the data needs to be corrected. The calculation formula of sample variance:
Insert picture description here
the syntax of variance:

VAR ( [ ALL | DISTINCT ] expression ) 
VARP ( [ ALL | DISTINCT ] expression ) 

Syntax for standard deviation:

STDEV ( [ ALL | DISTINCT ] expression ) 
STDEVP ( [ ALL | DISTINCT ] expression ) 

The difference between functions with Pand without P: VARand STDEVrefers to the variance and standard deviation of the population, VARPand the sum STDEVPrefers to the variance and standard deviation of the sample.


MSSQL function- STDEVstandard deviation and STDEVPstandard deviation

StDevThe function is to calculate the standard deviation of all the values ​​of the specified field in the query result. It is a program used to measure the deviation of the data from the arithmetic mean. The smaller the standard deviation, the less these values ​​deviate from the mean, and vice versa. This function does not calculate text values ​​and logical values ​​(such as TRUEsums FALSE). It reflects the degree of dispersion of the data relative to the average. For example, find the Nstandard deviation of each value and the square sum of the difference between each data and the average value divided by ( N-1) and then open the root sign.
Insert picture description here

StDevPThe function is to calculate the standard deviation of all the values ​​of the specified field in the query result, which is used to measure the degree of dispersion of the data set (also called the deviation from the mean). The smaller the standard deviation, the smaller the fluctuation of this group of data. Conversely, the larger the standard deviation, the greater the fluctuation of this set of data. The algorithm is seeking Nthe number of standard deviations from the mean for each data divided by the square of the difference between Nthe square root, a bit like a standard deviation from the calculated data with the standard deviation about the same.
Insert picture description here

StDevThe standard deviation calculated by the function is a program used to measure the deviation of the data from the arithmetic mean. The StDevPcalculated standard deviation is used to measure the degree of dispersion of the data set. Sometimes it is understood as the same, but there are subtle differences. The data is also There are some differences.
Insert picture description here

STDEVPFunction and STDEVfunction as in does not OVERand ORDER BYwhen used in conjunction with deterministic function clauses whose return value. And if OVERand ORDER BYwhen used with the clause, it is uncertain. This is the characteristic of all aggregate functions, and we must pay attention here.


sqlserverFind the standard deviation, oraclefind the standard deviation

oracle find the standard deviation:

select  STDdEV(column1) from (
select 50 column1 from dual
union
select 90  from dual
union
select 70  from dual) tt

Find the standard deviation in sqlserver:

select  STDEV(column1) from (
select 50 column1
union
select 90 
union
select 70 ) tt

Guess you like

Origin blog.csdn.net/WuLex/article/details/114661769