1.聚集函数(分组函数)有哪些?
AVG(x)
COUNT(x) //记录行数
MAX(x)
MEDIAN(x) //取中值
MIN(x)
STDDEV(x) //求标准差
SUM(x)
VARIANCE(x) //求方差
聚集函数可以对多行数据进行操作,通常返回一行结果,因此在使用聚集函数时需要注意单行多行的问题
■ The Oracle database has two main groups of functions: single-row functions and
aggregate functions.
■ Single-row functions operate on one row at a time and return one row of output for
each input row. There are five main types of single-row functions: character functions,
numeric functions, conversion functions, date functions, and regular expression
functions.
■ Aggregate functions operate on multiple rows and return one row of output.
■ Blocks of rows may be grouped together using the GROUP BY clause.
■ Groups of rows may be filtered using the HAVING clause.
2.SELECT语句的执行顺序(含聚集函数即分组函数)
SQL中使用分组函数(GROUP FUNCTIONS)时,我们常常引入HAVING子句来进一步限制条件(受WHERE子句位置限制),这样SELECT语句就变长了,通常是这样一个格式:
SELECT column,group_function //group函数之外的列必须处在group by子句中,也就是说column必须二选一
FROM table
[WHERE condition] //where子句中不能包含group函数,因为它先于group函数执行
[GROUP BY group_by_expression]
[HAVING group_condition]
[ORDER BY column] //只有order by下可以使用别名
那么他们的执行顺序是什么呢?
STEP 1:先决定显示哪些列,SELECT
STEP 2:从哪个表,FROM
STEP 3:再加入条件,WHERE子句
STEP 4:开始分组,GROUP BY子句
STEP 5:执行分组函数,GROUP FUNTION
STEP 6:加入分组后条件,HAVING子句
STEP 7:按照哪个列排序显示,ORDER BY子句
如图所示: