求SQL

hubinasm 2016-07-12 10:39:10
问题描述:
现有TA表,按照DateType的类型设置相关的日期;如果TA表的相关任务的日期延期,则以该任务做为前置任务的所有任务都需要延期(DateDays的数值是固定的)。求相关SQL。(其他:可能1是2的前置任务,2又是5,6,12的前置任务)


TA说明:
DateType如果为1,则为工作日,需要从Calendar的IsWork为1的做为项的天数
DateType如果为2,则为日历日,不需要从Calendar表读取(或者可以读取Calendar所有的日期)

TB说明:
PrefixTAID:前置任务

SQL描述如下:
create table TA(TAID int,StartDate datetime,EndDate datetime,DateDays int,DateType int);
create table TB(TBID int identity(1,1),TAID int,PrefixTAID int);
create table Calendar(CDate datetime,IsWork int);
go

insert into TA(TAID,StartDate,EndDate,DateDays,DateType)
select 1,'2016-7-1','2016-7-7',5,1
union all
select 2,'2016-7-8','2016-7-8',1,2;
go

insert into TB(TAID,PrefixTAID)
select 2,1
go

insert into Calendar(CDate,IsWork)
select '2016-7-1',1
union
select '2016-7-2',0
union
select '2016-7-3',0
union
select '2016-7-4',1
union
select '2016-7-5',1
union
select '2016-7-6',1
union
select '2016-7-7',1
union
select '2016-7-8',1
union
select '2016-7-9',0
union
select '2016-7-10',0
union
select '2016-7-11',1
union
select '2016-7-12',1
union
select '2016-7-13',1
union
select '2016-7-14',1
union
select '2016-7-15',1
go
...全文
837 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
hubinasm 2016-11-15
  • 打赏
  • 举报
回复
求 解 答
hubinasm 2016-07-25
  • 打赏
  • 举报
回复
引用 9 楼 ap0405140 的回复:

-- 更新前
select * from TA

/*
TAID        StartDate               EndDate                 DateDays    DateType
----------- ----------------------- ----------------------- ----------- -----------
1           2016-07-01 00:00:00.000 2016-07-07 00:00:00.000 5           1
2           2016-07-08 00:00:00.000 2016-07-08 00:00:00.000 1           2

(2 row(s) affected)
*/


-- 建函数
create function dbo.fn_getNewdate
(@fdate datetime,
 @delay int,
 @type int) returns datetime
as
begin
 declare @y datetime,@i int
 select @y=@fdate,@i=1
 
 if (@type=1)
 begin
   while(@i<=@delay)
   begin
     select @y=dateadd(d,1,@y)
     if exists(select 1 from Calendar where CDate=@y and IsWork=1)
     begin
       select @i=@i+1
     end
   end
 end
 else
 begin
   select @y=dateadd(d,@delay,@y)
 end
 
 return @y
end


-- 更新
declare @taid int,@delay int

select @taid=1,   -- 任务ID
       @delay=2   -- 延期天数

update TA
 set StartDate=dbo.fn_getNewdate(StartDate,@delay,DateType),
     EndDate=dbo.fn_getNewdate(EndDate,@delay,DateType)
 where TAID=@taid
 or TAID in(select TAID from TB where PrefixTAID=@taid)


-- 更新后
select * from TA

/*
TAID        StartDate               EndDate                 DateDays    DateType
----------- ----------------------- ----------------------- ----------- -----------
1           2016-07-05 00:00:00.000 2016-07-11 00:00:00.000 5           1
2           2016-07-10 00:00:00.000 2016-07-10 00:00:00.000 1           2

(2 row(s) affected)
*/
1是2的前置任务,所以只有1做完了才能做2,也就是说2的日期2016-7-12 2016-7-12 如果1不是2的前置任务,上面的是对的。 任务里面有些有前置任务,有些没有前置任务。
唐诗三百首 2016-07-21
  • 打赏
  • 举报
回复

-- 更新前
select * from TA

/*
TAID        StartDate               EndDate                 DateDays    DateType
----------- ----------------------- ----------------------- ----------- -----------
1           2016-07-01 00:00:00.000 2016-07-07 00:00:00.000 5           1
2           2016-07-08 00:00:00.000 2016-07-08 00:00:00.000 1           2

(2 row(s) affected)
*/


-- 建函数
create function dbo.fn_getNewdate
(@fdate datetime,
 @delay int,
 @type int) returns datetime
as
begin
 declare @y datetime,@i int
 select @y=@fdate,@i=1
 
 if (@type=1)
 begin
   while(@i<=@delay)
   begin
     select @y=dateadd(d,1,@y)
     if exists(select 1 from Calendar where CDate=@y and IsWork=1)
     begin
       select @i=@i+1
     end
   end
 end
 else
 begin
   select @y=dateadd(d,@delay,@y)
 end
 
 return @y
end


-- 更新
declare @taid int,@delay int

select @taid=1,   -- 任务ID
       @delay=2   -- 延期天数

update TA
 set StartDate=dbo.fn_getNewdate(StartDate,@delay,DateType),
     EndDate=dbo.fn_getNewdate(EndDate,@delay,DateType)
 where TAID=@taid
 or TAID in(select TAID from TB where PrefixTAID=@taid)


-- 更新后
select * from TA

/*
TAID        StartDate               EndDate                 DateDays    DateType
----------- ----------------------- ----------------------- ----------- -----------
1           2016-07-05 00:00:00.000 2016-07-11 00:00:00.000 5           1
2           2016-07-10 00:00:00.000 2016-07-10 00:00:00.000 1           2

(2 row(s) affected)
*/
hubinasm 2016-07-19
  • 打赏
  • 举报
回复
有人回答吗??求
hubinasm 2016-07-18
  • 打赏
  • 举报
回复
求 SQL
唐诗三百首 2016-07-13
  • 打赏
  • 举报
回复
“原先是2016-7-1 - 2016-7-7, 延期2天后,为何会"更新成2016-7-5----2016-7-11” 因为DateType为1,而Calendar表设置了7-2 7-3 7-9 7-10是休息日,所以扣除这几天,更新成了2016-7-5----2016-7-11 --> 扣除7-9, 7-10后, 结束日期好像不应该是2016-7-11..
hubinasm 2016-07-13
  • 打赏
  • 举报
回复
引用 3 楼 ap0405140 的回复:
比如TAID为1的任务延期2天,则 TAID为1的任务会更新成2016-7-5----2016-7-11 --> 请问"TAID为1的任务延期2天"的原始数据(即数字2)存在哪里? 原先是2016-7-1 - 2016-7-7, 延期2天后,为何会"更新成2016-7-5----2016-7-11"?
延期2天的这个2是存储过程里的传入参数而已;只要有延期,所有相关的任务都要延期; 任务延期根据DateType的属性决定。 “原先是2016-7-1 - 2016-7-7, 延期2天后,为何会"更新成2016-7-5----2016-7-11” 因为DateType为1,而Calendar表设置了7-2 7-3 7-9 7-10是休息日,所以扣除这几天,更新成了2016-7-5----2016-7-11 而TaID为2的任务因为DateType为2,不需要查找Calendar表,但是需要TaID为1的任务做完,所以 TaID为2的任务会更新成2016-7-12--2016-7-12
hubinasm 2016-07-13
  • 打赏
  • 举报
回复
引用 5 楼 ap0405140 的回复:
“原先是2016-7-1 - 2016-7-7, 延期2天后,为何会"更新成2016-7-5----2016-7-11” 因为DateType为1,而Calendar表设置了7-2 7-3 7-9 7-10是休息日,所以扣除这几天,更新成了2016-7-5----2016-7-11 --> 扣除7-9, 7-10后, 结束日期好像不应该是2016-7-11..
原先是2016-7-1 - 2016-7-7,工作日只有5天 延期2天后,变成了2016-7-5----2016-7-11,工作日还是5天,所以不矛盾
hubinasm 2016-07-12
  • 打赏
  • 举报
回复
最终要的结果: 如果TA表的相关任务的日期延期,则以该任务做为前置任务的所有任务都需要延期。 比如TAID为1的任务延期2天,则 TAID为1的任务会更新成2016-7-5----2016-7-11 TAID为2的任务因为前置任务是1,所以也需要延期,更新成2016-7-12----2016-7-12
--小F-- 2016-07-12
  • 打赏
  • 举报
回复
最终想得到什么结果呢?
唐诗三百首 2016-07-12
  • 打赏
  • 举报
回复
比如TAID为1的任务延期2天,则 TAID为1的任务会更新成2016-7-5----2016-7-11 --> 请问"TAID为1的任务延期2天"的原始数据(即数字2)存在哪里? 原先是2016-7-1 - 2016-7-7, 延期2天后,为何会"更新成2016-7-5----2016-7-11"?

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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