黑马程序员_.NET学习9(第六章)

来源:互联网 发布:xlplayer mac有杂音 编辑:程序博客网 时间:2024/05/16 16:56

---------------------- Windows Phone 7手机开发.Net培训、.NET学习型技术博客、期待与您交流! ----------------------


--聚合函数的应用
--求和函数:sum()
--示例  查询年龄大于40岁的教师的工资总数
select sum(salary)as 工资总数 from teacher where Age>=40

--计算函数:count()
count()的两种使用形式如下;
--count(*),计算表中行的总数,即使表中行的数据为null,也被计算在内。
--count(column),计算column列包含的行的数目,如果该列中某行数据为null,则改行不计入统计总数。
--查询教师表中的所有记录的行数。

select count(*) as totalltem from teacher

--查询教师表的多列中所有记录的行数。

select count(No) as tolal_No,count(Name) as total_Name from teacher

--查询教师中所有女教师的人数。

select count(*) as total_women from teacher where sex='女'


最大/最小值函数:max()/min()
求最大值或最小值的数据可以是数值,字符串或日期时间类型。
--示例  查询教师表中最大的年龄

select max(age) as maxAge from teacher

--查询教师表中最大的年龄教师的相关信息

select TName, DName,TSex,Sal,Age from teacher where Age=(select Max(Age) from teacher)

--时间求法

select min(ctest) as 最早,max(ctest) as 最晚 from course

--平均值函数:AVG()
AVG()函数的执行过程实际上是将一列中的值加起来,然后在除以非null值的数目。所以,与sum()函数一样,AVG()只能作用于数值类型数据。
--示例  查询教师的平均年龄

select avg(age) as 平均年龄 from teacher

--如果不对列中的所有值求平均,则可在where子句中使用搜索条件用于计算平均值的行。

--示例  查询计算机系的教师的平均年龄

select avg(age)as 平均年龄 from teacher where department='计算机系'

--示例  查询所有年龄高于平均年龄的教师信息

select * from teacher where Age > (select avg(age) from teacher)

--聚合函数的组合使用
--查询记录行数,最高工资,最低年龄,工资总和,平均工资

select count(*) as 记录行数,
       max(salary) as 最高工资,
       min(age) as 最低年龄,
       sum(salary) as 工资总和,
       avg(salary) as 平均工资,    from teacher    

group by子句创建分组
--语法
select column, sum(column) from table group by column

--查询所有男教师的平均工资和女教师的平均工资

select sex + '教师' as 教师类别,avg(salary) as 平均工资 from teacher group by sex


group by 子句根据多列组合行
--示例  查询各个系中男教师和女教师的人数。

select department,sex + '教师' as 教师类型,count(*) as 人数 from teacher group by department,sex  order by department

rollup 运算符和cube 运算符
rollup运算符的使用
--示例   查询各个系中男教师和女教师的人数。

select department,sex + '教师' as 教师类型, count(*) as 人数 from teacher group by department,sex with rollup order by department


cube运算符的使用
--示例  查询各个系中男教师和女教师的人数。

select department, sex + '教师' as 教师类型, count(*) as 人数from teacher group by department ,sex with cube order by department


having子句

--查询至少有两名教师的系名称以及教师的人数。

select department, count(*) as 人数 from teacher
group by department
having count(*)>=2

--查询至少拥有2名男教师的系名称以及男教师的人数。

select department,count(*)as 人数 from teacher
where sex='男'
group by department
having count(*)>=2




---------------------- Windows Phone 7手机开发.Net培训、.NET学习型技术博客、期待与您交流! ----------------------

 

 

详细请查看:http://net.itheima.com/