作者clonk (咚)
看板Database
标题Re: [SQL ] 工作排程天数由sql算出
时间Mon Jul 13 14:37:02 2015
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语法表示?
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 118.165.9.20
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Database/M.1436769429.A.506.html
※ 编辑: clonk (118.165.9.20), 07/13/2015 17:03:42