Content navigation
1. Definitions
Average values or expression is non-empty and the number of rows in the divided group.
2, Grammar
AVG function syntax is as follows:
AVG( [DISTINCT | ALL] expr )
This syntax can be broken down into the following form:
- AVG (DISTINCT expr)
different values of expr summed and divided by the number of unique occurrence. - AVG (ALL expr)
a non-null value expr each row is added, the number of non-blank lines and obtained by dividing the groups. - AVG (expr)
and AVG (ALL expr) equivalent.
Note: The
data type of the parameter expr must be a NUMBER.
3, Code
/*
作者:AT阿宝哥
日期:2016年9月18日
愿景:参考官方资料,做最好的课程,成就更多职业人!
邮箱:[email protected]
CSDN:https://blog.csdn.net/goldentec
简书:https://www.jianshu.com/u/8a6075d7a2e0
说明:
注意:
*/
-------------------------------------------------------------------------------
--Sample1:原封不动的返回字面量.
SELECT avg(2) FROM emp;
SELECT avg(3) FROM emp;
-------------------------------------------------------------------------------
--Sample2:
select sum(sal)/count(sal) from emp;
select avg(sal) from emp;
-------------------------------------------------------------------------------
--Sample3:
SELECT sum(DISTINCT sal)/count(DISTINCT sal) FROM emp;
SELECT avg(DISTINCT sal) FROM emp;
-------------------------------------------------------------------------------
--Sample4:
SELECT avg(comm) FROM emp;
-------------------------------------------------------------------------------