Maybe something like this. If not, then please provide consumable test data and expected results.-- *** Test Data ***CREATE TABLE #t( StartTime datetime NOT NULL ,Machine char(2) NOT NULL ,Condition int NOT NULL ,Duration int NOT NULL);INSERT INTO #tVALUES('20140104 05:55:00', 'A1', 3, 5) ,('20140104 06:55:00', 'A1', 2, 10) ,('20140104 07:55:00', 'A1', 3, 5) ,('20140104 09:55:00', 'A1', 5, 17) ,('20140204 05:55:00', 'A1', 3, 5) ,('20140204 06:55:00', 'A2', 2, 10) ,('20140204 07:55:00', 'A1', 3, 5) ,('20140204 09:55:00', 'A2', 5, 17);-- *** End Test Data ***SELECT StartTime, Machine ,COALESCE([1], 0) AS Sum1 ,COALESCE([2], 0) AS Sum2 ,COALESCE([3], 0) AS Sum3 ,COALESCE([4], 0) AS Sum4 ,COALESCE([5], 0) AS Sum5FROM ( SELECT DATEADD(day, DATEDIFF(day, 0, StartTime), 0) AS StartTime ,Machine, Condition, Duration --WHERE StartTime - DATEDIFF(day, 0, StartTime) BETWEEN '19000101 01:30' AND '19000101 22:30' FROM #t) SPIVOT( SUM (Duration) FOR Condition IN ([1],[2],[3],[4],[5])) AS PORDER BY StartTime, Machine;