Re: [SQL ] 工作排程天数由sql算出

楼主: clonk (咚)   2015-07-13 14:37:02
select *,前置工作天数=convert(int,0) into #temp from #资料表
while exists(select * from #temp where 前置工作项目1<>'' or 前置工作项目2<>'')
begin
update t1
set 前置工作项目1=case when t2.工作项目 is not null
then '' else t1.前置工作项目1 end,
前置工作项目2=case when t3.工作项目 is not null
then '' else t1.前置工作项目2 end,
前置工作天数=t1.前置工作天数+isnull(t2.工作天数+t2.前置工作天数,0)
+isnull(t3.工作天数+t3.前置工作天数,0)
from #temp t1
left join #temp t2 on t1.前置工作项目1=t2.工作项目
and t2.前置工作项目1='' and t2.前置工作项目2=''
left join #temp t3 on t1.前置工作项目2=t3.工作项目
and t3.前置工作项目1='' and t2.前置工作项目2=''
where t1.前置工作项目1<>'' or t1.前置工作项目2<>''
end
select 工作项目,第几天开始=前置工作天数,第几天结束=前置工作天数+工作天数
from #temp
※ 引述《chermany (qq)》之铭言:
: 数据库名称:SQL server
: 数据库版本:2012
: 内容/问题描述:
: 请问我有个资料表如下:
: 工作项目 前置工作项目1 前置工作项目2 工作天数
: ================================================================
: A 1
: B A 3
: C A B 5
: D A 2
: 我想得到:
: 工作项目 第几天开始 第几天结束
: ================================================================
: A 0 1
: B 1 4
: C 4 9
: E 1 3
: 每个工作项目都需要前置工作1、2项目全部结束后才能开始,前置工作项
: 目的结束天数=衔接的工作项目的开始天数,这样结果是否能使用sql语法表示?

Links booklink

Contact Us: admin [ a t ] ucptt.com