- 我的电脑系统:Windows 10 64位
- SQL Server 软件版本: SQL Server 2014 Express
本篇博客里面使用了 scott
库,如何你现在还没有添加这个库到你的服务器里面,请在查看本篇博客前,访问这篇博文来在你的服务器里面附加scott
库。
函数的分类
单行函数
多行函数
例子:
1
2
| select lower(ename) from emp;
--最终返回的是14行
|

1
2
| select max(sal) from emp;
--返回时1行 max() 是多行函数
|

聚合函数的分类
max()
求最大值
min()
求最小值
avg()
平均值
count()
求个数
count(*)
:返回表中所有的记录的个数
count(字段名)
: 返回字段值非空的记录的个数, 重复的记录也会被当做有效记录
count(distinct 字段名)
:返回字段不重复并且非空的记录的个数
1
2
| select count(*) from emp;
--返回emp表所有记录的个数
|

1
2
| select count(deptno) from emp;
--返回值是14,这说明deptno重复的记录也被当做有效的记录
|

1
2
| select count(distinct deptno) from emp;
--返回值是3, 统计deptno不重复的记录的个数
|

1
2
| select count(comm) from emp;
--返回值是4。 这说明comm为null的记录不会被当做有效的记录
|

注意的问题
1
| select max(sal), min(sal), count(*) from emp;
|

1
| select max(sal) "最高工资", min(sal) "最低工资", count(*) "员工人数" from emp;
|

1
2
| select max(sal), lower(ename) from emp;
--error 单行函数和多行函数不能混用
|
