SQL sever数据库----聚合函数

----聚合函数------------------------------
 --(1)员工总人数
 select count(*) 人数 from People
 --(2)最大工资
 select max(PeopleSalary) 最大工资 from People
 --(3)最小工资
 select min(PeopleSalary) 最小工资 from People
 --(4)所有员工的工资总和
 select sum(PeopleSalary) 工资总和 from People
 --(5)所有员工的平均工资
 select round(avg(PeopleSalary),2)平均工资 from People
 --(6)求数量,最大值,最小值,总会,平均值,在一行显示
 select count(*) 人数,max(PeopleSalary) 最大工资,min(PeopleSalary) 最小工资,sum(PeopleSalary) 工资总和,round(avg(PeopleSalary),2)平均工资
 from People
 --(7)武汉地区的员工人数,总工资,最高工资,最低工资和平均工资
 select count(*) 人数,max(PeopleSalary) 最大工资,min(PeopleSalary) 最小工资,sum(PeopleSalary) 工资总和,round(avg(PeopleSalary),2)平均工资
 from People
where PeopleAddress='武汉'
--(8)求工资比平均工资高的人员信息
select * from People
where PeopleSalary>(select round(avg(PeopleSalary),2)平均工资 from People)
--(9)求数量,年龄最大值,年龄最小值,年龄总和,年龄平均值,在一行表示
--1
--select *,year(getdate())-year(PeopleBirth) from people
select count(*) 人数,
max(year(getdate())-year(PeopleBirth)) 年龄最大值,
min(year(getdate())-year(PeopleBirth)) 年龄最小值,
sum(year(getdate())-year(PeopleBirth)) 年龄总和,
round(avg(year(getdate())-year(PeopleBirth)),2)平均年龄
 from People
--2
--select datediff(year,PeopleBirth,getdate()) 
--from People
select count(*) 人数,
max(datediff(year,PeopleBirth,getdate()) ) 年龄最大值,
min(datediff(year,PeopleBirth,getdate()) ) 年龄最小值,
sum(datediff(year,PeopleBirth,getdate()) ) 年龄总和,
round(avg(datediff(year,PeopleBirth,getdate()) ),2)平均年龄
 from People
 --(10)计算月薪在10000以上的男性员工的最大年龄,最小年龄和平均年龄
 select max(year(getdate())-year(PeopleBirth)) 年龄最大值,
 min(year(getdate())-year(PeopleBirth)) 年龄最小值,
 round(avg(year(getdate())-year(PeopleBirth)),2)平均年龄
 from People
 where PeopleSalary>10000 and PeopleSex='男'
 --(11)统计"武汉"或"上海"地区所有女员工的数量以及最大年龄,最小年龄,平均年龄
 select '武汉或上海女员工' 描述,count(*) 人数,
max(year(getdate())-year(PeopleBirth)) 年龄最大值,
min(year(getdate())-year(PeopleBirth)) 年龄最小值,
round(avg(year(getdate())-year(PeopleBirth)),2)平均年龄
 from People
 where (PeopleAddress in('武汉','上海')) and PeopleSex='女'
 --(12)年龄比平均年龄高的员工信息
 select * from People
 where (year(getdate())-year(PeopleBirth))
 >(select avg(year(getdate())-year(PeopleBirth)) from People)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

婧婧子♔♔♔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值