各位大神和老师,请问一下,以下问题能不能用python/SQL/VBA/其它任何语言代码,只要能解决一下。
“根据上面图标红色那两列判断有没有超时,规则为:
1、工作日内4小时内审批完才算不超时
2、剔除时间包括双休日、节假日、中午12:00-13:30,晚上18:00-早上8:30,双休日和节假日如果不能系统判断就手动创建集合”
最终也需要将结果输出到EXCEL表对应的同一行上,跪求操作方式和代码。(之前用EXCEL函数算的,太麻烦还总有漏的地方,比如两个日期有一个是节假日的时候)
ALTER FUNCTION [dbo].[节假日用时]
(
@s datetime,@e datetime
)
RETURNS int
AS
BEGIN
DECLARE @result int,@min int,@p int
SELECT @result = 0,@min = DATEDIFF(minute,@s,@e)
set @p=(@min-1)/2048+1
;with t as (
select a.number+b.number*2048 as n
from (
select number
from master..spt_values
where type='p'
) a
,(
select number
from master..spt_values
where type='p' and number<@p
) b
)
select @result = (
select COUNT(0)
from (
select *,(case
-- <510 分钟表示为8:30之前
when pass_min<510
then 0
-- >1080 分钟表示为18:00之后
when pass_min>=1080
then 0
-- 720-810表示 12:00-13:30
when pass_min>=720 and pass_min<810
then 0
-- 节假日自行建立holidy表
--when 上班='否'
--then 0
-- weekday 1、7 表示周六日
when DATEPART(WEEKDAY,curr_day) in (1,7)
--and 上班='否'
then 0
else 1
end
) as 工时
from (
select *,DATEDIFF(minute,curr_day,curr_minute) as pass_min
--,isnull(b.是否上班,'否') as 上班
from (
select DATEADD(minute,n,@s) as curr_minute
,CONVERT(date,DATEADD(minute,n,@s)) as curr_day
from t
where n<@min
) a
--left join holiday b on DATEPART(year,curr_day)=b.year and DATEPART(month,curr_day)=b.month and DATEPART(day,curr_day)=b.day
) a
) a
where 工时=0
)
RETURN @result
END
上边那个太繁琐了,换了个计算方式,最后 工时=0 ,统计非用工时间分钟数,工时=1,统计用工时间分钟数
=0是上边的sql不用改,=1,就直接去掉个字段,这个值就可以作为用时分钟使用,非用工用时就用不到了
CREATE FUNCTION [dbo].[节假日用时]
(
@s datetime,@e datetime
)
RETURNS int
AS
BEGIN
DECLARE @result int,@sd date,@ed date,@t datetime
SELECT @result = 0,@sd = CONVERT(date,@s),@ed=convert(date,@e)
if @sd<>@ed
begin
-- 扣除到达时间当天非用工用时早
select @t=dateadd(minute,30,dateadd(hour,8,convert(datetime,@sd)))
set @result = @result + (case when @t>@s then DATEDIFF(MINUTE,@s,@t) else 0 end)
-- 扣除到达时间当天非用工用时晚
select @t=dateadd(hour,18,convert(datetime,@sd))
set @result = @result + DATEDIFF(MINUTE,(case when @t>@s then @t else @s end),DATEADD(day,1,@sd))
-- 剔除当天中午12:00-13:30
-- 扣除审批时间当天非用工用时
select @t=dateadd(minute,30,dateadd(hour,8,convert(datetime,@ed)))
set @result = @result + datediff(minute,@ed,(case when @t>@e then @e else @t end))
select @t=dateadd(hour,18,convert(datetime,@ed))
set @result = @result +(case when @t>@e then 0 else DATEDIFF(MINUTE,@e,@t) end)
-- 剔除当天中午12:00-13:30
declare @d int
select @d = DATEDIFF(day,@sd,@ed)
if @d>1
begin
set @result = @result + DATEDIFF(MINUTE,@sd,DATEADD(day,@d-1,@sd))
set @result = @result - (
select COUNT(0)
-- ,b.是否上班
from (
select DATEADD(day,number+1,@sd) as pass_day
from master..spt_values
where type='p' and number<@d-1
) a
--left join holiday b on DATEPART(year,pass_day)=b.year and DATEPART(month,pass_day)=b.month and DATEPART(day,pass_day)=b.day
where (
DATEPART(WEEKDAY,pass_day) in (1,7)
-- and isnull(b.是否上班,'否')='是'
)
-- and b.是否上班='是'
) * 570
end
end
else
begin
-- 扣除到达时间当天非用工用时早
select @t=dateadd(minute,30,dateadd(hour,8,convert(datetime,@sd)))
set @result = @result + (case when @t>@s then DATEDIFF(MINUTE,@s,@t) else 0 end)
end
RETURN @result
END
写了个大概的,你自己把内容补全把,中午的没计算,法定节假日的没计算,周六日的没完善
使用的话比较简单
with t as (
select 1 as 序号,'xxxx1' as 审批编号,'xxxx' as 审批名称,'郭大' as 审批人,'提交申请' as 审批职责,'2021-5-25 17:03' as 到达时间,'2021-5-25 17:03' as 审批时间
union all select 2,'xxxx1','xxxx','黄老邪','审批','2021-5-25 17:03','2021-5-25 17:13'
union all select 3,'xxxx1','xxxx','隔壁老王','审批','2021-5-25 17:13','2021-5-26 8:43'
union all select 4,'xxxx1','xxxx','杨洋','Espace权限审核','2021-5-26 8:43','2021-5-26 9:03'
union all select 1,'xxxx2','xxxx','张三','审批','2021-5-25 17:03','2021-5-25 17:03'
union all select 2,'xxxx2','xxxx','黄老邪','审批','2021-5-25 17:03','2021-5-25 17:13'
union all select 3,'xxxx2','xxxx','隔壁老王','审批','2021-5-25 17:13','2021-5-26 5:43'
union all select 4,'xxxx2','xxxx','杨洋','Espace权限审核','2021-5-26 5:43','2021-5-26 9:03'
union all select 1,'xxxx3','xxxx','张三','审批','2021-5-28 17:03','2021-5-28 17:03'
union all select 2,'xxxx3','xxxx','黄老邪','审批','2021-5-28 17:03','2021-5-28 17:13'
union all select 3,'xxxx3','xxxx','隔壁老王','审批','2021-5-28 17:13','2021-5-31 5:43'
union all select 4,'xxxx3','xxxx','杨洋','Espace权限审核','2021-5-31 5:43','2021-5-31 9:03'
)
select *
,DATEDIFF(MINUTE,到达时间,审批时间) as 总用时
,dbo.节假日用时(到达时间,审批时间) as 非用工用时
,DATEDIFF(MINUTE,到达时间,审批时间)-dbo.节假日用时(到达时间,审批时间) as 用时分钟
,(DATEDIFF(MINUTE,到达时间,审批时间)-dbo.节假日用时(到达时间,审批时间))/60 as 用时小时
from t
----
序号 审批编号 审批名称 审批人 审批职责 到达时间 审批时间 总用时 非用工用时 用时分钟 用时小时
----------- ----- ---- -------- -------------- --------------- --------------- ----------- ----------- ----------- -----------
1 xxxx1 xxxx 郭大 提交申请 2021-5-25 17:03 2021-5-25 17:03 0 0 0 0
2 xxxx1 xxxx 黄老邪 审批 2021-5-25 17:03 2021-5-25 17:13 10 0 10 0
3 xxxx1 xxxx 隔壁老王 审批 2021-5-25 17:13 2021-5-26 8:43 930 870 60 1
4 xxxx1 xxxx 杨洋 Espace权限审核 2021-5-26 8:43 2021-5-26 9:03 20 0 20 0
1 xxxx2 xxxx 张三 审批 2021-5-25 17:03 2021-5-25 17:03 0 0 0 0
2 xxxx2 xxxx 黄老邪 审批 2021-5-25 17:03 2021-5-25 17:13 10 0 10 0
3 xxxx2 xxxx 隔壁老王 审批 2021-5-25 17:13 2021-5-26 5:43 750 703 47 0
4 xxxx2 xxxx 杨洋 Espace权限审核 2021-5-26 5:43 2021-5-26 9:03 200 167 33 0
1 xxxx3 xxxx 张三 审批 2021-5-28 17:03 2021-5-28 17:03 0 0 0 0
2 xxxx3 xxxx 黄老邪 审批 2021-5-28 17:03 2021-5-28 17:13 10 0 10 0
3 xxxx3 xxxx 隔壁老王 审批 2021-5-28 17:13 2021-5-31 5:43 3630 2443 1187 19
4 xxxx3 xxxx 杨洋 Espace权限审核 2021-5-31 5:43 2021-5-31 9:03 200 167 33 0
(12 行受影响)
可以通过SQL语句加逻辑判断实现,但要先创建双休日和节假等过滤数据。
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632