聚合函數(shù)在數(shù)據(jù)庫數(shù)據(jù)的查詢分析中,應(yīng)用十分廣泛。本節(jié)將分別對各聚合函數(shù)的應(yīng)用進行說明。
- 求和函數(shù)——SUM()
求和函數(shù)SUM( )用于對數(shù)據(jù)求和,返回選取結(jié)果集中所有值的總和。語法如下。
SELECTSUM(column_name)
FROMtable_name
說明:SUM()函數(shù)只能作用于數(shù)值型數(shù)據(jù),即列column_name中的數(shù)據(jù)必須是數(shù)值型的。
計數(shù)函數(shù)——COUNT()
COUNT()函數(shù)用來計算表中記錄的個數(shù)或者列中值的個數(shù),計算內(nèi)容由SELECT語句指定。使用COUNT函數(shù)時,必須指定一個列的名稱或者使用星號,星號表示計算一個表中的所有記錄。兩種使用形式如下。
COUNT(*),計算表中行的總數(shù),即使表中行的數(shù)據(jù)為NULL,也被計入在內(nèi)。
COUNT(column),計算column列包含的行的數(shù)目,如果該列中某行數(shù)據(jù)為NULL,則該行不計入統(tǒng)計總數(shù)。
使用COUNT(*)函數(shù)對表中的行數(shù)計數(shù)
COUNT(*)函數(shù)將返回滿足SELECT語句的WHERE子句中的搜索條件的函數(shù)。
使用COUNT( )函數(shù)對一列中的數(shù)據(jù)計數(shù)
COUNT( )函數(shù)可用于對一列中的數(shù)據(jù)值計數(shù)。與忽略了所有列的COUNT(*)函數(shù)不同,COUNT()函數(shù)逐一檢查一列(或多列)中的值,并對那些值不是NULL的行計數(shù)。
使用COUNT( )函數(shù)對多列中的數(shù)據(jù)計數(shù)
COUNT()函數(shù)不僅可用于對一列中的數(shù)據(jù)值計數(shù),也可以對多列中的數(shù)據(jù)值計數(shù)。如果對多列計數(shù),則需要將要計數(shù)的多列通過連接符連接后,作為COUNT()函數(shù)的參數(shù)。
使用COUNT函數(shù)對滿足某種條件的記錄計數(shù)
也可以在SELECT語句中添加一些子句約束來指定返回記錄的個數(shù)。最大/最小值函數(shù)—MAX()/MIN()
當需要了解一列中的最大值時,可以使用MAX()函數(shù);同樣,當需要了解一列中的最小值時,可以使用MIN()函數(shù)。語法如下。
SELECTMAX (column_name) / MIN (column_name)
FROMtable_name
說明:列column_name中的數(shù)據(jù)可以是數(shù)值、字符串或是日期時間數(shù)據(jù)類型。MAX()/MIN()函數(shù)將返回與被傳遞的列同一數(shù)據(jù)類型的單一值。
均值函數(shù)——AVG()
函數(shù)AVG()用于計算一列中數(shù)據(jù)值的平均值。語法如下。
SELECT AVG (column_name)
FROMtable_name
說明:AVG()函數(shù)的執(zhí)行過程實際上是將一列中的值加起來,再將其和除以非NULL值的數(shù)目。所以,與SUM()函數(shù)一樣,AVG()函數(shù)只能作用于數(shù)值型數(shù)據(jù),即列column_name中的數(shù)據(jù)必須是數(shù)值型的。
聚合分析的重值處理
前面介紹的5種聚合函數(shù),可以作用于所選列中的所有數(shù)據(jù)(不管列中的數(shù)據(jù)是否有重置),也可以只對列中的非重值進行處理,即把重復(fù)的值只取一次進行聚合分析。當然,對于MAX()/MIN()函數(shù)來講,重值處理意義不大。
可以使用ALL關(guān)鍵字指明對所選列中的所有數(shù)據(jù)進行處理,使用DISTINCT關(guān)鍵字指明對所選列中的非重值數(shù)據(jù)進行處理。以AVG()函數(shù)為例,語法如下。
SELECT AVG ([ALL/DISTINCT] column_name)
FROMtable_name
說明:[ALL/DISTINCT]在缺省狀態(tài)下,默認是ALL關(guān)鍵字,即不管是否有重值,處理所有數(shù)據(jù)。其他聚合函數(shù)的用法與此相同。
聚合函數(shù)的組合使用
select s_subject 科目,sum(s_score)各科總成績,max(s_score)單科最高分, min(s_score)單科最低分,
avg (s_score)單科平均分 from student_score wheres_score>60 group by s_subject;--分組查詢各科成績
實例練習(xí)代碼如下:
use t ;
create table student_score(s_id int primary key ,
s_name varchar(20),s_scoreint);--在數(shù)據(jù)庫t中創(chuàng)建表單student,同時增加s_id,s_age和s_score三列
select * from student_score;--查看 student_score 表單所有數(shù)據(jù)
--drop table student_score;--刪除table表單
insert into student_score (s_id,s_name,s_score) values(1,'jr',68);--增加初始值數(shù)據(jù)
insert into student_score (s_id,s_name,s_score) values(2,'jj',63);--增加初始值數(shù)據(jù)
insert into student_score (s_id,s_name,s_score) values(3,'mm',70);--增加初始值數(shù)據(jù)
insert into student_score (s_id,s_name,s_score) values(4,'mmj',80);--增加初始值數(shù)據(jù)
insert into student_score (s_id,s_name,s_score) values(5,'乞丐',69);--增加初始值數(shù)據(jù)
insert into student_score (s_id,s_name,s_score) values(6,'混混',90);--增加初始值數(shù)據(jù)
insert into student_score (s_id,s_name,s_score) values(7,'美女',75);--增加初始值數(shù)據(jù)
insert into student_score (s_id,s_name,s_score) values(8,'鳥人',70);--增加初始值數(shù)據(jù)
insert into student_score (s_id,s_name,s_score) values(9,'SB',33);--增加初始值數(shù)據(jù)
insert into student_score (s_id,s_name,s_score) values(10,'傻妞',59);--增加初始值數(shù)據(jù)
select * from student_score;--查看 student_score 表單所有數(shù)據(jù)
--使用聚合函數(shù)
select count (*) 學(xué)生個數(shù) from student_score;--count對行計數(shù)學(xué)生個數(shù)
select count (s_score) 學(xué)生個數(shù) fromstudent_score;--count對行計數(shù)有分數(shù)的學(xué)生個數(shù)
insert into student_score (s_id,s_name) values(11,'毛毛');--增加初始值數(shù)據(jù)
select count (*) 學(xué)生個數(shù) from student_score;--count對行計數(shù)學(xué)生個數(shù)
select count (s_score) 學(xué)生個數(shù) fromstudent_score;--count對行計數(shù)有分數(shù)的學(xué)生個數(shù)
select count(distinct s_score) 有分且不重復(fù)學(xué)生個數(shù) fromstudent_score;--計算有分,并且分數(shù)不相同的學(xué)生個數(shù)
select sum (s_score) 所有學(xué)生總分 fromstudent_score;--用聚合函數(shù)計算所有學(xué)生總分
select avg(s_score) 所有學(xué)生平均分 fromstudent_score;--用聚合函數(shù)計算所有學(xué)生平均分
select max(s_score) 所有學(xué)生最高分 fromstudent_score;--用聚合函數(shù)計算所有學(xué)生最高分
select min(s_score) 所有學(xué)生最低分 fromstudent_score;--用聚合函數(shù)計算所有學(xué)生最低分
select * from student_score;--查看 student_score 表單所有數(shù)據(jù)
--分組查詢
alter table student_score add s_subject varchar(20);--增加科目這一列
update student_score set s_subject ='語文';--把student_score表中所有科目設(shè)置為語文
select * from student_score;--查看 student_score 表單所有數(shù)據(jù)
update student_score set s_subject = '數(shù)學(xué)' wheres_idin('5','6','7','8');--把student_score表中id為'5','6','7','8'科目設(shè)置為數(shù)學(xué)
update student_score set s_subject = '英語' wheres_idin('9','10','11');--把student_score表中id為'9','10','11'科目設(shè)置為英語
select * from student_score;--查看 student_score 表單所有數(shù)據(jù)
select s_subject 科目 ,sum(s_score)各科總成績 fromstudent_score group by s_subject;--分組查詢各科總成績
select s_subject 科目 ,sum(s_score)各科總成績,max(s_score)單科最高分,min(s_score)單科最低分,
avg (s_score)單科平均分 from student_score wheres_score>60 group by s_subject;--分組查詢各科成績
select * from student_score;--查看 student_score 表單所有數(shù)據(jù)
--having子句
--begin transaction --開啟事務(wù)
update student_score set s_name ='sb';--把student_score表中所有名字改成jj
update student_score set s_name = 'jr' where s_idin('3','7','11');--把student_score表中id為'5','6','7','8'名字設(shè)置為jr
update student_score set s_name = 'mm' where s_idin('2','6','10');--把student_score表中id為'9','10','11'名字設(shè)置為mm
update student_score set s_name = 'ff' where s_idin('4','8');--把student_score表中id為'4','8'名字設(shè)置為ff
select s_name 姓名 ,sum(s_score)各科總成績,max(s_score)單科最高分,min(s_score)單科最低分,
avg (s_score)平均分 from student_score group bys_name ;--分組查詢各科成績
select s_name 姓名 ,sum(s_score)各科總成績,max(s_score)單科最高分,min(s_score)單科最低分,
avg (s_score)平均分 from student_score group bys_name having avg(s_score)>70;--分組查詢各科成績
select * from student_score;--查看 student_score 表單所有數(shù)據(jù)
--commit; --提交
--rollback; --回滾
愛華網(wǎng)



