数据库查询-聚合函数详解,聚合函数应用,分组查询

发布于:2024-05-24 ⋅ 阅读:(65) ⋅ 点赞:(0)

聚合函数

Sql Server中的聚合函数主要有:

count:求数量
max:求最大值
min:求最小值
sum:求和
avg:求平均值

聚合函数的应用

我们使用的是依旧是上次的数据库中的部门,职级,员工表,下面主要是对于员工表的数据查询

1.求员工总人数count
select count(*)from People
2.求最大值max,求最高工资
select max(PeopleSalary) 最高工资 from People
3.求最小值min,求最小工资
select min(PeopleSalary) 最低工资 from People
4.求和sum,求得所有员工的工资总和
select sum(PeopleSalary) 工资总和 from People
5.求平均值avg,求所有员工的平均工资
select avg(PeopleSalary) 平均工资 from People
6.求数量,最大值,最小值,综合,平均值,用到函数count,max,min,sum,avg
select count(PeopleSalary) 人员数量,max(PeopleSalary)最高工资,min(PeopleSalary)最低工资 ,sum(PeopleSalary)工资总和,avg(PeopleSalary)平均工资  from People
7.查询出武汉地区的员工人数,总工资,最高工资,最低工资和平均工资
-- 我们在使用聚合函数的时候一样是可以使用条件语句where的
select count(PeopleSalary) 人员数量,max(PeopleSalary)最高工资,min(PeopleSalary)最低工资 ,sum(PeopleSalary)工资总和,avg(PeopleSalary)平均工资  from People 
where PeopleAddress ='武汉'
8.求出工资比平均工资高的人员信息  -- 上文用到子查询,即在where条件中含有select
-- 人员信息是需要* from 的,所以我们需要先写出条件where
-- where PeopleSalary > (select round(avg(PeopleSalary),2) 平均工资 from People
-- round(参数1,参数2),round函数是可以使得浮点数更加精确的函数,参数1表示浮点数数值,参数二表示保留的小数点位数
select * from People where PeopleSalary > (select round(avg(PeopleSalary),2) 平均工资 from People
9.求数量,年龄最大值,年龄最小值,年龄综合,年龄平均值,在一行中显示
-- 年龄表达式:year(getdate()-PeopleBirth)
select count(*)数量,max(year(getdate()-PeopleBirth)) 最大年龄,min(year(getdate()-PeopleBirth))最小年龄,sum(year(getdate()-PeopleBirth))年龄总和,avg(year(getdate()-PeopleBirth))年龄平均值 from People
10.计算出月薪在10000以上的男性员工的最大年龄,最小年龄和平均年龄
select max(year(getdate()-PeopleBirth)) 最大年龄,min(year(getdate()-PeopleBirth))最小年龄,avg(year(getdate()-PeopleBirth))年龄平均值 from People where PeopleSalary >10000
11.统计出现所在地为武汉或者上海的所有女员工的数量和最大年龄
select count(*) max(year(getdate()-PeopleBirth)) 最大年龄 from People where PeopleAddress in ('武汉','上海') and PeopleSex = '女'

分组查询

根据某一列,进行分组,比如员工表中按照地区分组,然后在进行查询的方法

1.根据地区分组查询员工人数,最高工资和最低工资
select '武汉' 地区,count(*) 员工人数, max(PeopleSalary) 最高工资,min(PeopleSalary) 最低工资 from People where PeopleAddress = '武汉' 
-- 如果我们想要查多个地区一起显示呢,就要使用关键字union 表示链接,结合的意思
-- 比如 青岛和武汉的人
select '武汉' 地区,count(*) 员工人数, max(PeopleSalary) 最高工资,min(PeopleSalary) 最低工资 from People where PeopleAddress = '武汉' 
union 
select '青岛' 地区,count(*) 员工人数, max(PeopleSalary) 最高工资,min(PeopleSalary) 最低工资 from People where PeopleAddress = '青岛'
-- 如果将所有地区分组之后的数据进行显示,需要我们使用group by 分组依据(列)
select PeopleAddress 地区,count(*) 员工人数, max(PeopleSalary) 最高工资,min(PeopleSalary) 最低工资 from People group by PeopleAddress
-- group by 分组依据,这个分组依据是可以写select后面的,但是非聚合函数(能得到具体数值)以及非分组依据的,不能填写在select后面
2.根据员工所在地区分组统计员工人数,工资总和,平均工资,且1985年及其后出身的员工不参与统计
-- 分组查询使用group by 的时候,使用where条件查询的时候,where要在group by之前且where条件中不能使用聚合函数,
-- 使用聚合函数作为条件的时候,应该放在group by的后面 用关键字having 聚合函数
-- 如下:
select PeopleAddress 地区,count(*) 员工人数, max(PeopleSalary) 最高工资,min(PeopleSalary) 最低工资 from People 
where PeopleBirth < '1985-1-1'
group by PeopleAddress
having count(*) >=2  -- 表明查询数据的条件为出生日期在1985之前,且按照地区分组后,人数大于2,所以先普通条件where得到未分组前的数据,然后分组处理之后,再次通过having判断是否满足count(*)(聚合函数条件)

网站公告

今日签到

点亮在社区的每一天
去签到