2011-01-31 63 views
0

使用SQL Server。如何按條件拆分總時間

表1

ID TotalTime 

001 12:00:00 
002 11:00:00 
003 09:00:00 
004 08:00:00 
005 14:00:00 
.... 
...., 

TotalTime Datatype is nvarchar 

我需要的總時間柱拆分3列像下面的條件

First 8 Hours should display in First column 
After 8 Hours the next 2 Hours should display in Second column 
After 10 Hours the remaining hours should display in third column 

期望輸出

ID TotalTime Column1 Column2 Column3 

001 12:00:00 08:00:00 02:00:00 02:00:00 
002 11:00:00 08:00:00 02:00:00 01:00:00 
003 09:00:00 08:00:00 01:00:00 
004 08:00:00 08:00:00  
005 14:00:00 08:00:00 02:00:00 04:00:00 
.... 
...., 

如何使查詢上述條件。

需要查詢幫助。

+0

爲0,而不是空的2列3接受的價值? – 2011-01-31 10:40:55

+0

您的TotalTime列的類型是什麼?不像nvarchar,我希望...你能告訴我們表的定義嗎? – 2011-01-31 10:43:49

回答

2

你可以使用一個case分裂時間:

select ID 
,  case when totaltime > 8 then 8 
      else totaltime end 
,  case when totaltime > 10 then 2 
      when totaltime > 8 then totaltime - 8 
      else 0 end 
,  case when totaltime > 10 then totaltime - 10 
      else 0 end 
from YourTable