0

我的 StartTime 和 StopTime 任务记录很少,我想将其拆分为 2 小时的 4 条记录(8:30 - 10:30 / 10:30 - 12:30 / 13:00 - 15:00 / 15: 00 - 17:00)通常,我想拆分这一行:

From
Title StartTime            StopTime
Task1 2013-11-12 09:00     2013-11-12 14:00
To
Title StartTime            StopTime
Task1 2013-11-12 09:30     2013-11-12 10:30
Task1 2013-11-12 10:30     2013-11-12 12:30
Task1 2013-11-12 12:30     2013-11-12 14:00

欢迎任何建议,谢谢。

4

1 回答 1

0

试试这个查询:

SQLFiddle 演示

select [TaskName],
       CASE WHEN CONVERT(varchar,StartTime,108)<pStart
       THEN floor(CAST(StartTime as float))+Cast(pStart as Datetime)
       ELSE StartTime
       END as StartTime,
       CASE WHEN CONVERT(varchar,StopTime,108)>pEnd
       THEN floor(CAST(StopTime as float))+Cast(pEnd as Datetime)
       ELSE StopTime
       END as StopTime

FROM T       

JOIN 
(
  SELECT '08:30:00' as pStart, '10:30:00' as pEnd 
  UNION ALL
  SELECT '10:30:00' as pStart, '12:30:00' as pEnd
  UNION ALL
  SELECT '13:00:00' as pStart, '15:00:00' as pEnd  
  UNION ALL
  SELECT '15:00:00' as pStart, '17:00:00' as pEnd  
)  as DayParts 
  ON (CONVERT(varchar,StartTime,108)<=pEnd 
       AND 
      CONVERT(varchar,StopTime,108)>=pEnd
     ) 
     OR 
     (
      CONVERT(varchar,StopTime,108)>=pStart
       AND 
      CONVERT(varchar,StartTime,108)<=pStart   
     )  

ORDER BY TaskName,StartTime
于 2013-11-12T12:22:35.150 回答