本文主要是介绍数据库中写聚合函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
数据库中聚合函数是指max,min,avg,sum,count
聚合函数对null值不计算
如果一行的数据都是null,计算count(*)则计算null的行数
sql语句的执行顺序,
1>form表
2>where条件
3>group by 列
4>Having 条件
5>select distinct top
6>order by
–查询tsID降序排列的前五个学生
select top 5 * from TblStudent order by tsID desc
–查询结果合并为一列
select top 5
tsAddress+’ ‘+tsName as studentName
from TblStudent order by tsID desc
–按百分比求,向上取整
select top 10 percent * from TblStudent order by tsID desc
select distinct
tsName,tsAddress,tsClassID
from TblStudent
–查询TblStudent中tsAge的平均数,作为平均年龄这一列,三种写法
select AVG(tsAge) as 平均年龄 from TblStudent
select AVG(tsAge) 平均年龄 from TblStudent
select 平均年龄=AVG(tsAge) from TblStudent
select sum(tsAge) as 总年龄 from TblStudent
–null也被计算在内
select count(tsAge) as 记录总数 from TblStudent
–一个语句执行多条查询
select
count(tsAge) as 记录总数,
sum(tsAge) as 总年龄,
AVG(tsAge) as 平均年龄
from TblStudent
–子查询在查询里面查询查询语句
select * from
(select AVG(tsAge) as 平均年龄 from TblStudent) as a,
(select AVG(tsAge) 平均年龄 from TblStudent) as b,
(select 平均年龄=AVG(tsAge) from TblStudent ) as c
这篇关于数据库中写聚合函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!