日期:2014-05-18  浏览次数:20510 次

请假问题---如何判断有多少个工作天?
我司每天工作8小时,但上下班时间分多个班次.
8:00---12:00     13:30---17:30
8:00---11:30     13:30---18:00
8:00---11:45     13:30---17:45

若某人请假从2007/08/01   8:00----     2007/08/03   17:30   如何求出请假3天呢?
若某人请假从2007/08/01   8:00----     2007/08/03   17:45   如何求出请假3天呢?
若某人请假从2007/08/01   8:00----     2007/08/03   12:00   如何求出请假2.5天呢?

请赐教.谢谢

------解决方案--------------------
将一月的休息日放到一个表中.然后根据日期,时间来算具体的请假时间.
------解决方案--------------------
这里有个类似的例:关于 周六 周日 的 数据过滤


表A 数据如下:

SYS_ID 人员 部门 到达时间 离开时间
----------------------------------------------------
0001 小王 技术部 2006-1-2 2006-1-6
0002 小王 技术部 2006-1-4 2006-1-10
0003 小沈 运销部 2006-1-2 2006-1-3
0004 小沈 运销部 2006-1-2 2006-1-6
0005 小李 技术部 2006-1-9 2006-1-13
0006 小张 技术部 2006-1-11 2006-1-21
0007 小马 技术部 2006-2-1 2006-2-12
0007 小马 技术部 2006-4-1 2006-4-9
.......................................................

现在 想查询以下数据:(部门为技术部的人员停留时间超过2天和超过3天的记录总数(周六和周日不能计算在内)):结果应如下:

人员 超过3天 超过5天
小王 ?? ??
小李 。。。 。。。
小张
小马
................................

大家说 周六 和周日 如何过滤掉啊?


create table A(SYS_ID char(4), Name nvarchar(10), Dept nvarchar(10), BegTime datetime, EndTime datetime)
insert A select '0001 ', '小王 ', '技术部 ', '2006-1-2 ', '2006-1-6 '
union all select '0002 ', '小王 ', '技术部 ', '2006-1-4 ', '2006-1-10 '
union all select '0003 ', '小沈 ', '运销部 ', '2006-1-2 ', '2006-1-3 '
union all select '0004 ', '小沈 ', '运销部 ', '2006-1-2 ', '2006-1-6 '
union all select '0005 ', '小李 ', '技术部 ', '2006-1-9 ', '2006-1-13 '
union all select '0006 ', '小张 ', '技术部 ', '2006-1-11 ', '2006-1-21 '
union all select '0007 ', '小马 ', '技术部 ', '2006-2-1 ', '2006-2-12 '
union all select '0007 ', '小马 ', '技术部 ', '2006-4-1 ', '2006-4-9 '

create function fun(@BegTime datetime, @EndTime datetime)
returns int
as
begin
declare @re int
set @re=0
while @BegTime <=@EndTime
begin
select @re=@re+1 where (datepart(weekday, @BegTime)+@@datefirst-1)%7 between 1 and 5
set @BegTime=dateadd(day, 1, @BegTime)
end

return @re
end

select Name,
超过3天=sum(case when days> 3 then 1 else 0 end),
超过5天=sum(case when days> 5 then 1 else 0 end)
from(
select Name, dbo.fun(BegTime, EndTime) as days
from A
where Dept= '技术部 '
)a group by Name

--result
Name 超过3天 超过5天
---------- ----------- -----------
小李 1 0
小马 2 1
小王 2 0
小张 1 1

(4 row(s) affected)

------解决方案--------------------
那要看你的表结构了