加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

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

(2012-01-01 00:41:04)
标签:

sql

统计

工资

部门

emp

oracle

server

mysql

报表

分类: JAVA高级软件工程师
部门名称 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

http://s14/middle/4f9ce8f3gb5aa330b975d&690

第二部:

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
http://s6/middle/4f9ce8f3gb5aa3841fb25&690

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
 
该文对我是否有帮助最多可选1项
发起时间:2012-01-01 01:00    截止时间:2012-03-01 00:00    投票人数:0人
  • 0(0%)
  • 0(0%)
  • 0(0%)
  • 0(0%)
投票已截止
最后投票

    0

    阅读 收藏 喜欢 打印举报/Report
      

    新浪BLOG意见反馈留言板 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

    新浪公司 版权所有