聚合函数,也可以叫做聚组函数、组函数、集合函数,具体包括:count、sum、avg、max、min、stddev、variance。

【特别说明】:使用聚合函数进行统计时,只有count函数会把空值一并统计,除此以外的其他聚合函数都不会对空值进行统计。

一.创建演示数据

为演示聚合(聚组)函数的使用效果,现在创建一张样例表,建表语句如下:

-- Create table
create table STUDENTRECORDTABLE
(
  studentid VARCHAR2(50) not null,
  classid   VARCHAR2(50),
  grade     NUMBER
)
tablespace TPSTUD
  pctfree 10
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );
-- Add comments to the table 
comment on table STUDENTRECORDTABLE
  is '学生成绩表(演示)';
-- Add comments to the columns 
comment on column STUDENTRECORDTABLE.studentid
  is '学生id';
comment on column STUDENTRECORDTABLE.classid
  is '班级id';
comment on column STUDENTRECORDTABLE.grade
  is '成绩';
-- Create/Recreate primary, unique and foreign key constraints 
alter table STUDENTRECORDTABLE
  add constraint PK_STUDENTRECORDTABLE primary key (STUDENTID)
  using index 
  tablespace TPSTUD
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );

再往表中插入数据,语句如下:

insert into STUDENTRECORDTABLE (studentid, classid, grade)
values ('Stud_01', 'Cla_01', 75);
insert into STUDENTRECORDTABLE (studentid, classid, grade)
values ('Stud_02', 'Cla_01', 80);
insert into STUDENTRECORDTABLE (studentid, classid, grade)
values ('Stud_03', 'Cla_01', 80);
insert into STUDENTRECORDTABLE (studentid, classid, grade)
values ('Stud_04', 'Cla_01', 85);
insert into STUDENTRECORDTABLE (studentid, classid, grade)
values ('Stud_05', 'Cla_02', 90);
insert into STUDENTRECORDTABLE (studentid, classid, grade)
values ('Stud_06', 'Cla_02', 95);
insert into STUDENTRECORDTABLE (studentid, classid, grade)
values ('Stud_07', 'Cla_02', 95);
insert into STUDENTRECORDTABLE (studentid, classid, grade)
values ('Stud_08', 'Cla_02', 100);
commit;

最后查询表中所有数据,结果如下图:

 二.聚合(聚组)函数讲解

1.count:统计数据数量

【函数格式】count( * | value | [ distinct | all ] column )

【参数说明】

  • * :固定值星号“*”,表示统计包括空值在内的满足条件的所有记录的数量;
  • value :可以为具体的数值或字符串,表示统计包括空值在内的满足条件的所有记录的数量;
  • distinct:表示只统计不重复出现的数据,该参数可选;
  • all:表示统计包括空值在内的满足条件的所有数据,该参数可选;
  • column :需要统计的具体字段名。

【函数说明】:该函数统计满足条件的所有记录的数量,当省略参数distinctall时,默认为all

【样例展示一】

select count(*) from STUDENTRECORDTABLE;        --返回:8
select count(1) from STUDENTRECORDTABLE;        --返回:8
select count('aa') from STUDENTRECORDTABLE;        --返回:8
select count(grade) from STUDENTRECORDTABLE;        --返回:8
select count(all grade) from STUDENTRECORDTABLE;        --返回:8
select count(distinct grade) from STUDENTRECORDTABLE;        --返回:6

【样例展示二】与group by函数配合使用

select classid,count(*) from STUDENTRECORDTABLE group by classid

返回结果如下图:

【样例展示三】与group by函数以及having子句配合使用

select classid,count(*) from STUDENTRECORDTABLE group by classid having count(*)=4

 返回结果如下图:

2.sum:统计数据总和

【函数格式】sum( [ distinct | all ] column )

【参数说明】

  • distinct:表示只统计不重复出现的数据,该参数可选;
  • all:表示统计满足条件的所有数据,该参数可选;
  • column :需要统计的具体字段名,必须为数值型字段。

【函数说明】:对所有满足条件的记录的指定字段进行求和,当省略参数distinctall时,默认为all

【样例展示一】

select sum(grade) from STUDENTRECORDTABLE;        --返回:700
select sum(all grade) from STUDENTRECORDTABLE;        --返回:700
select sum(distinct grade) from STUDENTRECORDTABLE;        --返回:525

【样例展示二】与group by函数配合使用

select classid,sum(grade) from STUDENTRECORDTABLE group by classid

 返回结果如下图:

【样例展示三】与group by函数以及having子句配合使用

select classid,sum(grade) from STUDENTRECORDTABLE group by classid having sum(grade)>350

 返回结果如下图:

3.avg:统计数据平均值

【函数格式】avg( [ distinct | all ] column )

【参数说明】

  • distinct:表示只统计不重复出现的数据,该参数可选;
  • all:表示统计满足条件的所有数据,该参数可选;
  • column :需要统计的具体字段名,必须为数值型字段。

【函数说明】:对所有满足条件的记录的指定字段求平均值,当省略参数distinctall时,默认为all

【样例展示一】

 select avg(grade) from STUDENTRECORDTABLE;        --返回:87.5
select avg(all grade) from STUDENTRECORDTABLE;        --返回:87.5
select avg(distinct grade) from STUDENTRECORDTABLE;        --返回:87.5

【样例展示二】与group by函数配合使用

select classid,avg(grade) from STUDENTRECORDTABLE group by classid

 返回结果如下图:

【样例展示三】与group by函数以及having子句配合使用

select classid,avg(grade) from STUDENTRECORDTABLE group by classid having avg(grade)>90

 返回结果如下图:

4.max:统计数据最大值

【函数格式】max( [ distinct | all ] column )

【参数说明】

  • distinct:表示只统计不重复出现的数据,该参数可选;
  • all:表示统计满足条件的所有数据,该参数可选;
  • column :需要统计的具体字段名,可以为数值型、字符型或日期型字段。

【函数说明】:对所有满足条件的记录的指定字段求最大值,当省略参数distinctall时,默认为all

【样例展示一】

select max(grade) from STUDENTRECORDTABLE;        --返回:100
select max(all grade) from STUDENTRECORDTABLE;        --返回:100
select max(distinct grade) from STUDENTRECORDTABLE;        --返回:100
select max(studentid) from STUDENTRECORDTABLE;        --返回:Stud_08
select max(classid) from STUDENTRECORDTABLE;        --返回:Cla_02

【样例展示二】与group by函数配合使用

select classid,max(grade) from STUDENTRECORDTABLE group by classid

 返回结果如下图:

【样例展示三】与group by函数以及having子句配合使用

select classid,max(grade) from STUDENTRECORDTABLE group by classid having max(grade)>90

 返回结果如下图:

5.min:统计数据最小值

【函数格式】min( [ distinct | all ] column )

【参数说明】

  • distinct:表示只统计不重复出现的数据,该参数可选;
  • all:表示统计满足条件的所有数据,该参数可选;
  • column :需要统计的具体字段名,可以为数值型、字符型或日期型字段。

【函数说明】:对所有满足条件的记录的指定字段求最小值,当省略参数distinctall时,默认为all

 【样例展示一】

select min(grade) from STUDENTRECORDTABLE;        --返回:75
select min(all grade) from STUDENTRECORDTABLE;        --返回:75
select min(distinct grade) from STUDENTRECORDTABLE;        --返回:75
select min(studentid) from STUDENTRECORDTABLE;        --返回:Stud_01
select min(classid) from STUDENTRECORDTABLE;        --返回:Cla_01

【样例展示二】与group by函数配合使用

select classid,min(grade) from STUDENTRECORDTABLE group by classid

 返回结果如下图:

 【样例展示三】与group by函数以及having子句配合使用

select classid,min(grade) from STUDENTRECORDTABLE group by classid having min(grade)<80

 返回结果如下图:

6.stddev:统计数据标准差

【函数格式】stddev( [ distinct | all ] column )

【参数说明】

  • distinct:表示只统计不重复出现的数据,该参数可选;
  • all:表示统计满足条件的所有数据,该参数可选;
  • column :需要统计的具体字段名,必须为数值型字段。

【函数说明】:对所有满足条件的记录的指定字段求标准差,当省略参数distinctall时,默认为all

 【样例展示一】

select stddev(grade) from STUDENTRECORDTABLE;        --返回:8.86405260427918
select stddev(all grade) from STUDENTRECORDTABLE;        --返回:8.86405260427918
select stddev(distinct grade) from STUDENTRECORDTABLE;  --返回:9.35414346693485

 【样例展示二】与group by函数配合使用

select classid,stddev(grade) from STUDENTRECORDTABLE group by classid

 返回结果如下图:

 【样例展示三】与group by函数以及having子句配合使用

select classid,stddev(grade) from STUDENTRECORDTABLE group by classid having stddev(grade)<5

 返回结果如下图:

 7.variance:统计数据方差

【函数格式】variance( [ distinct | all ] column )

【参数说明】

  • distinct:表示只统计不重复出现的数据,该参数可选;
  • all:表示统计满足条件的所有数据,该参数可选;
  • column :需要统计的具体字段名,必须为数值型字段。

【函数说明】:对所有满足条件的记录的指定字段求方差,当省略参数distinctall时,默认为all

 【样例展示一】

select variance(grade) from STUDENTRECORDTABLE;        --返回:78.5714285714286
select variance(all grade) from STUDENTRECORDTABLE;      --返回:78.5714285714286
select variance(distinct grade) from STUDENTRECORDTABLE;        --返回:87.5

 【样例展示二】与group by函数配合使用

select classid,variance(grade) from STUDENTRECORDTABLE group by classid

 返回结果如下图:

 【样例展示三】与group by函数以及having子句配合使用

select classid,variance(grade) from STUDENTRECORDTABLE group by classid having variance(grade)>16

 返回结果如下图:

 

 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐