常用的聚合函數 1 count 2 sum 3 avg 4 max 5 min 使用規范時 默認的是all distinct 指定所有的唯一非空值行 count (行的數目) select count ( 列或者*) from 表 sum (總和) select sum(計數規范)from
分組 (group by) select 列a , 聚合函數 from 表 where 過濾條件 group by 列a (以列a來分組)
過濾聚合函數(having) select 列a , 聚合函數 from 表 where 過濾條件 group by 列a having 聚合函數過濾條件
要注意的是 SQL語句的執行順序 (5)select 列a , 聚合函數 (1) from 表 (2) where 過濾條件 (3) group by 列a (4) having 聚合函數過濾條件 (6) order by
課后練習 select * from nobel – 1獲獎總人數 select count (distinct winner) from nobel – 2 諾貝爾 物理獎的獲獎總次數 select count(subject)from nobel where subject=’Physics’ – 3 顯示每個獎項的獲獎總次數 select subject,count(subject)from nobel group by subject – 4顯示每個獎項第一次獲得的年份 select min(yr) 年份,subject 獎項 from nobel group by subject – 5 顯示每個獎項在2000年來獲得的人數 select subject 獎項,yr 年份 from nobel where yr=2000 group by subject ,yr – 6 顯示每個獎項不同獲獎者的人數 select subject 獎項,count (distinct winner)from nobel group by subject – 7 顯示每個獎項 有多少年獲獎 select subject 獎項,count (distinct yr)from nobel group by subject – 8 顯示當年有三個Physic的年份 select subject 獎項,yr 年份 from nobel where subject=’Physics’ group by yr, subject having count(winner)=3 – 9 得獎大于1的winner select winner 獲獎者 from nobel group by winner having count(subject)>1 –10 得到多個獎項的winner select winner 獲獎者 from nobel group by winner having count(distinct subject)>1 –11
新聞熱點
疑難解答