看板 Database 關於我們 聯絡資訊
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://www.ptt.cc/bbs/Database/M.1436769429.A.506.html ※ 編輯: clonk (118.165.9.20), 07/13/2015 17:03:42