常用的聚合函数 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
新闻热点
疑难解答