部门平均工资mysql_按部门统计各工资级别的人数、平均工资。

本文介绍了如何使用MySQL查询按部门统计各工资级别的员工人数及平均工资。通过连接UserSalary、SalaryGrade、UserDeptJob和Institution四张表,实现对员工工资分布和部门平均工资的分析。最终展示结果包括技术部、业务部和客户服务部等各个部门的详细统计信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

部门名称

0--1000

1000--2000

2000--3000

3000--5000

5000+

平均工资

技术部

0

0

3

10

5

4000

业务部

1

1

6

4

8

5000

客户服务部

1

1

10

2

1

2800

1.需求:在一个办公系统中,有一个决策分析的功能。有一子模块,是按部门统计各个工资级别的人数、平均工资。

部门名称

0--1000

1000--2000

2000--3000

3000--5000

5000+

平均工资

技术部

0

0

3

10

5

4000

业务部

1

1

6

4

8

5000

客户服务部

1

1

10

2

1

2800

2.涉及到的表:

2.1 UserSalary:用户工资表.

字段为:用户编号(userId),基本工资(salary1)。数据如:sd100301,5000

2.2 SalaryGrade:工资级别表.

字段为:工资级别(grade),级别起点(losal),级别终点(hisal)。数据如:1,0,1000;2,1000,2000

2.3 UserDeptJob:用户部门职位表。

字段为:userId(用户编号),institutionId(部门编号),jobId(职位编号)

2.4 Institution:部门表。

字段为:institutionId(部门编号),部门名称(institutionName)

3.SQL:

第一 步:

select max(s.grade) as grade,count(e.userId) as

peopleCount,

i.institutionName--,e.salary1 as salary1

from Salarygrade s left join UserSalary e

on e.salary1 between s.losal and hisal

join UserDeptJob udj

on udj.userId=e.userId

join institution i

on i.institutionId=udj.institutionId

where e.salary1>0

group by s.grade,i.institutionName--,e.salary1

a4c26d1e5885305701be709a3d33442f.png

第二部:

select institutionName,

nvl(max(decode(grade,'1',peopleCount)),0) as

first, nvl(max(decode(grade,'2',peopleCount)),0) as second ,

nvl(max(decode(grade,'3',peopleCount)),0) as third,

nvl(max(decode(grade,'4',peopleCount)),0) as fouth ,

nvl(max(decode(grade,'5',peopleCount)),0) as fifth

--,avg(salary1)

from

(

select max(s.grade) as grade,count(e.userId) as peopleCount,

i.institutionName--,e.salary1 as salary1

from Salarygrade s left join UserSalary e

on e.salary1 between s.losal and hisal

join UserDeptJob udj

on udj.userId=e.userId

join institution i

on i.institutionId=udj.institutionId

where e.salary1>0

group by s.grade,i.institutionName--,e.salary1

)

group by institutionName

a4c26d1e5885305701be709a3d33442f.png

SQL说明:连接员工工资表和工资基本表,可以统计出员工在各个工资级别的分布;再连接部门表,即可按部门统计工资的级别分布情况。

4.结果:

部门名称

0--1000

1000--2000

2000--3000

3000--5000

5000+

平均工资

技术部

0

0

3

10

5

4000

业务部

1

1

6

4

8

5000

客户服务部

1

1

10

2

1

2800

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值