分析函数-STDDEV,STDDEV_SAMP ,STDDEV_POP,VAR_POP,VAR_SAMP,VARIANCE

/*STDDEV:计算当前行关于组的标准偏离

STDDEV_SAMP : 该函数计算累积样本标准偏离,并返回总体变量的平方根

STDDEV与 stddev_samp 的不同之处在于,当计算的输入数据只有一行时,stddev 返回 0,而
stddev_samp 返回 null*/

计算公式:





select stddev(salary) FROM hr.employees
WHERE department_id =20;--结果为4949.74746830583

--STDDEV,STDDEV_SAMP
SELECT SQRT(SUM( POWER((SALARY - (SELECT AVG(SALARY) FROM hr.employees
WHERE department_id =20)), 2)
/((SELECT COUNT(1) FROM hr.employees
WHERE department_id =20)-1 )))
FROM hr.employees
WHERE department_id =20;--结果为4949.74746830583




---STDDEV_POP标准方差

--计算公式




select STDDEV_POP(salary) FROM hr.employees
WHERE department_id =20;--结果为3500

SELECT SQRT(SUM( POWER((SALARY - (SELECT AVG(SALARY) FROM hr.employees
WHERE department_id =20)), 2)
/((SELECT COUNT(1) FROM hr.employees
WHERE department_id =20))))
FROM hr.employees
WHERE department_id =20;--结果为3500


/*VAR_POP
功能描述:(Variance Population)该函数返回非空集合的总体变量(忽略 null),VAR_POP
进行如下计算:
(SUM(expr2) - SUM(expr)2 / COUNT(expr)) / COUNT(expr)*/
select var_pop(salary) FROM hr.employees
WHERE department_id =20----结果为12250000

select count(1)/*COUNT(expr)*/ FROM hr.employees WHERE department_id =20;

select (sum(power(salary,2)) - power(sum(salary),2)/2)/2
FROM hr.employees WHERE department_id =20;----结果为12250000


/*VAR_SAMP
功能描述:(Variance Sample)该函数返回非空集合的样本变量(忽略 null),VAR_SAMP 进
行如下计算:
(SUM(expr*expr)-SUM(expr)*SUM(expr)/COUNT(expr))/(COUNT(expr)-1)*/

select var_samp(salary) FROM hr.employees
WHERE department_id =20--结果为24500000

select count(1)/*COUNT(expr)*/ FROM hr.employees WHERE department_id =20;

select (sum(power(salary,2)) - power(sum(salary),2)/2)/(2-1)
FROM hr.employees WHERE department_id =20--结果为24500000



/*VARIANCE
功能描述:该函数返回表达式的变量,Oracle 计算该变量如下:
如果表达式中行数为 1,则返回 0
如果表达式中行数大于 1,则返回 VAR_SAMP*/

select VARIANCE(salary) FROM hr.employees
WHERE department_id =10--表达式中行数为 1 结果为0

select VARIANCE(salary) FROM hr.employees
WHERE department_id =20--表达式中行数大于1 结果为24500000
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值