用组函数合计数据:
组函数的概念及应用 :
avg/sum/max/min/count/distinct/gruop by having/嵌套组函数
组函数又被称为多行函数:和单行函数不同,组函数将把多行值当作一个整体进行处理
Count(*) 返回表中满足select 语句标准的行数,包括重复行,包括有空值列的行。如果where子句包括在select 语句中,count(*)返回满足where 子句条件的行数。对比,count(expr)返回在列中的由expr指定的非空值的数。count(distinct expr)返回在列中的由expr 指定的唯一的非空值的数;
显示部门50中的雇员人数;如:
select col,count(0) from tmpl gruop by col order by col;
select count(col),count(distinct col) from tmpl;
select count(*) from employess where department_id=50;
select department_id,count(*) from employees group by department_id;
使用distinct关键字:
count(distinct expr)返回对于表达式expr 非空并且值不相同的行数,使用distinct 关键字禁止计算在一列中的重复值。显示employees表中不同部门数的值;
select count(distinct department_id)from employees;
ave();
select avg(nul(commission_pct,0)) from employees;
group by 子句;
select department_id ,avg(salary) from employees group by department_id;
总结:
在SQL中有7个组函数可用:
avg 平均值 、count 计数、max最大值、min最小值 、sum合计,stddev标准差,variance方差。
你可以用group by 子句创建子分组,分组可以用having子句约束。
在语句中将having和group by 子句放在where 子句的后面。
Oracle服务器按下面的顺序求子句的值:
1.如果语句包含一个where子句,服务器立侯选行。
2.服务器确定在group by 子句中指定的组;
3.having 子句进一步约束不满足在having 子句中分组标准的结果分组;