请教邹大哥一个问题,关于时间和工期的

zyg0 2005-04-28 10:59:03
--表的结构
create table xmrw
(
xmrwid bigint IDENTITY (1, 1),--自增主键
xmrwzt int,--休息状态 3周六休息 2周日休息 1周六周日休息 0,不修
xmrwday bigint, --工期
xmrwtime datetime --任务开始日期
)
insert into xmrw
select '0','3',getdate()
union
select '1','5',getdate()
union
select '3','2',getdate()
union
select '2','7',getdate()
union
select '0','4',getdate()
union
select '1','4',getdate()
union
select '3','5',getdate()
union
select '2','8',getdate()
union
select '0','7',getdate()
我想实现对项目结束日期的查询 项目结束日期 必须+上他在项目中休息的天数
如 他是4月1号开始 4月1日是星期5 工期是2天,他的结束日期就是 4月1日+2天+休息的2天=4月5日
...全文
167 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zyg0 2005-04-28
  • 打赏
  • 举报
回复
我忘记说了,工期字段中不包括周六周日
ly_0205 2005-04-28
  • 打赏
  • 举报
回复
这个应该可以:

SELECT *,
case xmrwzt
when 0 then xmrwtime + xmrwday
when 1 then xmrwtime + xmrwday / 5 * 7 + case when datepart(dw, xmrwtime)=1 then 1 when datepart(dw, xmrwtime) + xmrwday % 5 > 6 then 2 else 0 end + xmrwday % 5
when 2 then xmrwtime + xmrwday / 6 * 7 + case when datepart(dw, xmrwtime)=1 then 1 when datepart(dw, xmrwtime) + xmrwday % 6 > 7 then 1 else 0 end + xmrwday % 6
when 3 then xmrwtime + xmrwday / 6 * 7 + case when datepart(dw, xmrwtime)=1 then 0 when datepart(dw, xmrwtime) + xmrwday % 6 > 6 then 1 else 0 end + xmrwday % 6
end etime
FROM xmrw
xluzhong 2005-04-28
  • 打赏
  • 举报
回复
--表的结构
create table xmrw
(
xmrwid bigint IDENTITY (1, 1),--自增主键
xmrwzt int,--休息状态 3周六休息 2周日休息 1周六周日休息 0,不修
xmrwday bigint, --工期
xmrwtime datetime --任务开始日期
)
insert into xmrw
select '0','3',getdate()
union
select '1','5',getdate()
union
select '3','2',getdate()
union
select '2','7',getdate()
union
select '0','4',getdate()
union
select '1','4',getdate()
union
select '3','5',getdate()
union
select '2','8',getdate()
union
select '0','7',getdate()
/*
我想实现对项目结束日期的查询 项目结束日期 必须+上他在项目中休息的天数
如 他是4月1号开始 4月1日是星期5 工期是2天,他的结束日期就是 4月1日+2天+休息的2天=4月5日
我忘记说了,工期字段中不包括周六周日
休息状态 3周六休息 2周日休息 1周六周日休息 0,不修
*/
go
create function f_getendtime(@id int)
returns datetime
as
begin
declare @xmrwzt int
declare @xmrwday int
declare @endtime datetime
select @xmrwzt=xmrwzt,@xmrwday=xmrwday,@endtime=xmrwtime from xmrw where xmrwid=@id
select @xmrwday=@xmrwday+case when @xmrwzt=0 then 0
when @xmrwzt=1 then 1
when @xmrwzt=2 then 1
when @xmrwzt=3 then 2
end
while @xmrwday>0
begin
if datepart(weekday,@endtime)<=5
begin
set @endtime=dateadd(d,1,@endtime)
set @xmrwday=@xmrwday-1
end
else
begin
set @endtime=dateadd(d,1,@endtime)
end
end
return(@endtime)
end

go

select *,dbo.f_getendtime(xmrwid) from xmrw

go

drop function f_getendtime
drop table xmrw
xluzhong 2005-04-28
  • 打赏
  • 举报
回复
xmrwid xmrwzt xmrwday xmrwtime endtime
1 0 3 2005-04-28 14:12:01.763 2005-05-03 14:12:01.763
2 0 4 2005-04-28 14:12:01.763 2005-05-04 14:12:01.763
3 0 7 2005-04-28 14:12:01.763 2005-05-07 14:12:01.763
4 1 4 2005-04-28 14:12:01.763 2005-05-05 14:12:01.763
5 1 5 2005-04-28 14:12:01.763 2005-05-06 14:12:01.763
6 2 7 2005-04-28 14:12:01.763 2005-05-08 14:12:01.763
7 2 8 2005-04-28 14:12:01.763 2005-05-09 14:12:01.763
8 3 2 2005-04-28 14:12:01.763 2005-05-04 14:12:01.763
9 3 5 2005-04-28 14:12:01.763 2005-05-07 14:12:01.763
xluzhong 2005-04-28
  • 打赏
  • 举报
回复
--表的结构
create table xmrw
(
xmrwid bigint IDENTITY (1, 1),--自增主键
xmrwzt int,--休息状态 3周六休息 2周日休息 1周六周日休息 0,不修
xmrwday bigint, --工期
xmrwtime datetime --任务开始日期
)
insert into xmrw
select '0','3',getdate()
union
select '1','5',getdate()
union
select '3','2',getdate()
union
select '2','7',getdate()
union
select '0','4',getdate()
union
select '1','4',getdate()
union
select '3','5',getdate()
union
select '2','8',getdate()
union
select '0','7',getdate()
/*
我想实现对项目结束日期的查询 项目结束日期 必须+上他在项目中休息的天数
如 他是4月1号开始 4月1日是星期5 工期是2天,他的结束日期就是 4月1日+2天+休息的2天=4月5日
我忘记说了,工期字段中不包括周六周日
休息状态 3周六休息 2周日休息 1周六周日休息 0,不修
*/
go
create function f_getendtime(@id int)
returns datetime
as
begin
declare @xmrwzt int
declare @xmrwday int
declare @endtime datetime
select @xmrwzt=xmrwzt,@xmrwday=xmrwday,@endtime=xmrwtime from xmrw where xmrwid=@id
select @xmrwday=@xmrwday+case when @xmrwzt=0 then 2
when @xmrwzt=1 then 3
when @xmrwzt=2 then 3
when @xmrwzt=3 then 4
end
while @xmrwday>0
begin
set @endtime=dateadd(d,1,@endtime)
set @xmrwday=@xmrwday-1
end
return(@endtime)
end

go

select *,dbo.f_getendtime(xmrwid) from xmrw

go

drop function f_getendtime
drop table xmrw
zjcxc 元老 2005-04-28
  • 打赏
  • 举报
回复
--表的结构
create table xmrw
(
xmrwid bigint IDENTITY (1, 1),--自增主键
xmrwzt int,--休息状态 3周六休息 2周日休息 1周六周日休息 0,不修
xmrwday bigint, --工期
xmrwtime datetime --任务开始日期
)
insert into xmrw
select '0','3',getdate() union all
select '1','5',getdate() union all
select '3','2',getdate() union all
select '2','7',getdate() union all
select '0','4',getdate() union all
select '1','4',getdate() union all
select '3','5',getdate() union all
select '2','8',getdate() union all
select '0','7',getdate()
go

/*--增加天数的函数

指定日期添加/减少指定工作天数后的日期
@workday 为正数表示增加天数,为负数表示减少天数
如果 @dt 是周六,周日,而 @workday=0
则返回 >@dt 的第一个工作日

--邹建 2005.04(引用请保留此信息)--*/
create function f_dateadd(
@dt datetime, --要添加的日期
@workday int, --要增加的工作天数
@flag int --工作天数计算标志,3周六休息,2周日休息,1周六周日休息,0不修
)returns datetime
as
begin
declare @i int,@step int,@day1 int,@day2 int
if @flag=0
select @step=7,@day1=-1,@day2=-1
else if @flag=1
select @step=5,@day1=6,@day2=0
else if @flag=2
select @step=6,@day1=6,@day2=-1
else if @flag=3
select @step=6,@day1=-1,@day2=0

select @i=case when @workday<0 then -1 else 1 end
,@dt=dateadd(week,@workday/@step,@dt)
,@workday=@workday%@step
while @workday<>0
select @dt=dateadd(day,@i,@dt)
,@workday=case when (@@datefirst+datepart(weekday,@DT)-1)%7 in(@day1,@day2)
then @workday else @workday-@i end
while (@@datefirst+datepart(weekday,@DT)-1)%7 in(@day1,@day2)
select @dt=dateadd(day,@i,@dt)
return(@dt)
end
go

--调用函数实现查询
select *,endtime=dbo.f_dateadd(xmrwtime,xmrwday,xmrwzt)
from xmrw
go

--删除测试
drop table xmrw
drop function f_dateadd

/*--结果

xmrwid xmrwzt xmrwday xmrwtime endtime
-------- -------- -------- ------------------------- --------------------------
1 0 3 2005-04-28 13:49:48.780 2005-05-01 13:49:48.780
2 1 5 2005-04-28 13:49:48.780 2005-05-05 13:49:48.780
3 3 2 2005-04-28 13:49:48.780 2005-04-30 13:49:48.780
4 2 7 2005-04-28 13:49:48.780 2005-05-06 13:49:48.780
5 0 4 2005-04-28 13:49:48.780 2005-05-02 13:49:48.780
6 1 4 2005-04-28 13:49:48.780 2005-05-04 13:49:48.780
7 3 5 2005-04-28 13:49:48.780 2005-05-04 13:49:48.780
8 2 8 2005-04-28 13:49:48.780 2005-05-08 13:49:48.780
9 0 7 2005-04-28 13:49:48.780 2005-05-05 13:49:48.780

(所影响的行数为 9 行)
--*/
天地客人 2005-04-28
  • 打赏
  • 举报
回复
是有一定的难度,不过,肯定可以实现,最好是用过程来做!
Comer 2005-04-28
  • 打赏
  • 举报
回复
高难。。。

学习

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧