数据库函数
聚合函数(统计函数)
Select AVG(age) as 年龄 from biao where banji=’一班’
--直接出平均值,赋值给列名 年龄。注意as位置在from前. AVG只可以对数字类型操作。
Select COUNT(*) from biao where name like ‘王%’—王姓的人数
Select COUNT( distinct age) from biao –-去除年龄相同的。显示年龄的个数!
Select MAX/MIN (age) from biao where banji=’一班’—-一班最大/小的年龄
Select SUM(shuxue) from biao where name like ‘王%’—王姓所有人的数学分数 总和
()中都可以使用distinct ,不写时默认是 all
use student
go
select * from biao
select AVG(yuwen) as 语文平均分 from biao
select COUNT (*)as geshu from biao
select AVG (age) as 年龄 from biao where banji='一班'--求一班的平均年龄
select COUNT(*) as 个数 from biao --查询biao中数据个数
select COUNT (distinct banji) from biao --表示班级个数,去除重名!
select MAX (yuwen )from biao where banji='一班'--
select MIN (yuwen )from biao where banji='一班'--
select SUM (yuwen )from biao where age=23--
select AVG(age) as 平均年龄,COUNT(*)as 人数 from biao where banji='一班'
--当返回值是同一类型时,可以组合使用函数。如上显示,一班的平均年龄,人数。
select COUNT (*) from biao where age=25 --25 岁的人个数
select AVG (age)as 平均年龄, MAX (yuwen)as 语文最高分 from biao where sex='女'
update biao set name='田园青' where code between 1 and 2
--1,2两行的名字改成了田园青
select *from biao where yuwen >75 or shuxue>75 or yingyu>75 order by yuwen
--从biao中选取大于的,再按语文升序摆列
select banji from biao group by banji-- 分组,只显示一班和二班
select banji ,AVG(yuwen) as 语文平均分 from biao group by banji -- 先分组后求平均分
select age,COUNT(*) as 个数 from biao where age between 20 and 25 group by age
--20~25岁之间按年龄分组,并且显示出每个年龄的人数
select banji, count(*) as 个数 from biao where yuwen >=60 group by banji
--显示按班分组,每个班语文大于的个数
select banji, count(*) as 大于分的人数 from biao where yuwen >=88 group by banji having COUNT(*)>5
--按班级分组,再求每班语文分大于的人数,最后在选出符合前面条件人数大于的!
select banji ,COUNT(*) as 语文大于的人数 from biao where yuwen>=75 group by banji order by COUNT(*)desc
--选出语文成绩大于分班级的人数按班级分组,再按降序排列(group by 后面的语句最后执行)
select banji ,MAX (yuwen),MAX(shuxue),MAX(yingyu)from biao group by banji order by MAX (yuwen) asc
--按班级分组,求出语数英最大值,最后按语文分升序排列(order by 后面必须跟前面语句中的信息)
select banji ,COUNT(*) as 班人数 ,AVG (yuwen) as 每班语文平均分 from biao group by banji having AVG(yuwen)>85
--按班级分组,再求出班人数和每班语文平均分,最后再选取语文平均分大于的!
--先执行where,再group by(按…分组), 再执行前面的聚合函数,
--最后执行having 函数(having 函数+ 判断符+ 条件)
select code,name ,ABS(yuwen) as 语文分数绝对值 from biao
select code,name,yuwen from biao where ABS (yuwen)>95
--ABS 取绝对值。选取分数绝对值大于人的code, name, yuwen
select CEILING (shuxue)from biao where (shuxue-CEILING (shuxue))!=0--取上限!
--where语句,可以找出是哪个数值取得上限!
select FLOOR (shuxue)from biao where code=8--取下限!
select '这是'+name+'年龄的平方:',POWER (age,2)from biao
select POWER (4,3) as 呵呵 --表示的次方,命名为呵呵
select ROUND (1.765734211,1)--把.765734211四舍五入到小数点后位!
select shuxue,ROUND(shuxue,0)as shehou from biao where shuxue != ROUND (shuxue,0)
--显示出四舍五入后不相同的信息.
select SQRT (9) as 平方根 --求平方根。得出结果3.
select SQUARE (9) as 平方 --平方!得出结果是81.