2011-01-31 65 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