日期:2014-05-17  浏览次数:20432 次

在线等,希望大神们可以帮忙写个存储过程,愁了一下午了
有四个表

Member 人员表  
字段有:memberid 人员id,membername 人员姓名,deptnum 部门id,ssbid 机构id

dept 部门表
字段有:deptnum 部门id,deptname 部门名称,ssbid 机构id

subssb 机构表
字段有:ssbid 机构id,ssbname 机构名称

tasttime 工时表
字段有:workday 工作日期,status 任务状态,worktask 工时,memberid 人员id

注:人员的上级为部门,部门的上级为机构
工时表中status为3的是审核通过的工时,以下查询都是查询status为3审核通过的

现在我需要查询各个机构下面的各个部门上周(从周一到周日)工时利用率

需要显示的字段为:
1.部门名称,
2.该部门总人数,
3.人均工时(人/天)算法为:总工时/(总人数*5个工作日)
4.平均贡献率(%)算法为:部门所有人员上周总工时/(8个工作小时*5个工作日)
5.不足80%人员数量 个人平均贡献率低于80%的总数,算法为上周个人确认后的总工时/8个工作小时*5个工作日
6.不足80%人员比例 算法为:个人平均贡献率低于80%的人员总数/部门总人数

------解决方案--------------------
哈哈,你先一步一步实现咯.
------解决方案--------------------
总工时是什么?最好有个实例写出来。
我看到的就是字段,但我不知道字段里存的会不会有重复。。。
------解决方案--------------------
SQL code

select d.deptname '部门名称',
       isnull(m.ct,0) '部门总人数',
(select sum(worktask) from tasttime t
 inner join Member m on t.memberid=m.memberid
 where m.deptnum=d.deptnum and t.[status]=3
 and t.workday>=cast(datename(yyyy,getdate())+'-01-01 00:00:00' as datetime)
 and datepart(wk,t.workday)=datepart(wk,getdate())-1)/(isnull(m.ct,1)*5) '人均工时(人/天)',
(select sum(worktask) from tasttime t
 inner join Member m on t.memberid=m.memberid
 where m.deptnum=d.deptnum and t.[status]=3
 and t.workday>=cast(datename(yyyy,getdate())+'-01-01 00:00:00' as datetime)
 and datepart(wk,t.workday)=datepart(wk,getdate())-1)/(8*5) '平均贡献率(%)',
(select count(1) from tasttime t
 inner join Member m on t.memberid=m.memberid
 where m.deptnum=d.deptnum and t.[status]=3
 and t.workday>=cast(datename(yyyy,getdate())+'-01-01 00:00:00' as datetime)
 and datepart(wk,t.workday)=datepart(wk,getdate())-1
 group by t.memberid
 having cast(sum(worktask)/(8*5) as decimal(5,2))<0.8) '不足80%人员数量',
rtrim((select count(1) from tasttime t
 inner join Member m on t.memberid=m.memberid
 where m.deptnum=d.deptnum and t.[status]=3
 and t.workday>=cast(datename(yyyy,getdate())+'-01-01 00:00:00' as datetime)
 and datepart(wk,t.workday)=datepart(wk,getdate())-1
 group by t.memberid
 having cast(sum(worktask)/(8*5) as decimal(5,2))<0.8)/
 cast(isnull(m.ct,1) as decimal(5,2))*100)+'%' '不足80%人员比例'
from dept d 
left join
(select deptnum,count(1) 'ct'
 from Member group by deptnum) m
on d.deptnum=m.deptnum