数据库中的聚合函数

常用的聚合函数

  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

  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值