2017-05-25 66 views
0

我喜歡這個網站找到解決我的querys,但現在我找不到任何解決我的問題組時間戳,總結休息到一個小時

我有這個表:

START_TIME   END_TIME    GROUP_DATE  GROUP_ID 
24-04-2017 13:15 24-04-2017 13:28  24-04-2017  1 
24-04-2017 14:01 24-04-2017 15:04  24-04-2017  1 
24-04-2017 15:04 24-04-2017 20:00  24-04-2017  1 
24-04-2017 20:00 24-04-2017 21:35  24-04-2017  2 
25-04-2017 0:10 25-04-2017 1:25  25-04-2017  2 

,我試圖讓這個:

date   Hour dec_hour GROUP_ID 
... 
24-04-2017 10  0   1 
24-04-2017 11  0   1 
24-04-2017 12  0   1 
24-04-2017 13  0.22  1 
24-04-2017 14  0.98  1 
24-04-2017 15  1   1 
24-04-2017 16  1   1 
24-04-2017 17  1   1 
24-04-2017 18  1   1 
24-04-2017 19  1   1 
24-04-2017 20  1   1 
24-04-2017 21  0.59  2 
24-04-2017 22  0   2 
24-04-2017 23  0   2 
25-04-2017 00  0.83  2 
25-04-2017 01  0.41  2 
25-04-2017 02  0   2 
25-04-2017 03  0   2 
... 

,你可以看到,一小時是60分鐘的間隔切割,其餘的添加到一個小時,我可以建立一個TMEP表並插入記錄但我不知道如何分割跨度小時

我不知道我有點接近這一點:

select CAST(start_time AS Date) as LogDate 
, datepart(hh, start_time) as Hour 
, SUM (isnull (DATEDIFF(s,START_TIME,END_TIME),0)) as Entries 

FROM MY_DAM_TABLE 
WHERE ... 

Group By CAST(start_time AS Date) 
, datepart(hh, start_time) 

=

LogDate  Hour minuts 
2017-05-24 13  780 
2017-05-24 14  3774 
2017-05-24 15  17731 
2017-05-24 20  5710 

請,任何暗示將大大讚賞

感謝

回答

0

你可以使用一個理貨小時表和OUTER APPLY李柯本

DECLARE @SampleData AS TABLE 
(
    START_TIME datetime, 
    END_TIME datetime, 
    GROUP_DATE date, 
    GROUP_ID int 
) 

INSERT INTO @SampleData 
VALUES 
('2017-04-24 13:15', '2017-04-24 13:28', '2017-04-24', 1), 
('2017-04-24 14:01', '2017-04-24 15:04', '2017-04-24', 1), 
('2017-04-24 15:04', '2017-04-24 20:00', '2017-04-24', 1), 
('2017-04-24 20:00', '2017-04-24 21:35', '2017-04-24', 2), 
('2017-04-25 0:10', '2017-04-25 1:25', '2017-04-25', 2) 

DECLARE @StartHour datetime , @EndHour datetime 

SELECT @StartHour = min(sd.GROUP_DATE), 
     @EndHour = dateadd(day, 1, max (sd.GROUP_DATE)) 
FROM @SampleData sd 


-- temp returns all hours from @StartHour And @EndHour 
;WITH temp AS 
(
    SELECT @StartHour AS StartHour, dateadd(hour,1,@StartHour) AS EndHour 
    UNION ALL 
    SELECT dateadd(hour,1,t.StartHour), dateadd(hour,2,t.StartHour) 
    FROM temp t 
    WHERE t.StartHour < dateadd(hour, -1 ,@EndHour) 
) 
SELECT CAST(t.StartHour AS date) AS Date, 
     datepart(hour,t.StartHour) AS Hour, 
     CAST(ISNULL(SUM(datediff(minute,a.ActualStartTime,a.ActualEndTime)),0) AS decimal(10,2))/60 AS dec_hour , 
     min(a.GROUP_ID) AS Group_Id 
FROM temp t 
OUTER APPLY 
(
    SELECT CASE WHEN sd.START_TIME > t.StartHour THEN START_TIME ELSE t.StartHour END as ActualStartTime, 
      CASE WHEN sd.END_TIME > t.EndHour THEN t.EndHour ELSE sd.END_TIME END as ActualEndTime, 
      sd.GROUP_ID 
    FROM @SampleData sd 
    WHERE t.StartHour BETWEEN sd.START_TIME AND sd.END_TIME 
     OR (t.EndHour BETWEEN sd.START_TIME AND sd.END_TIME) 
     OR (sd.START_TIME BETWEEN t.StartHour AND t.EndHour) 
     OR (sd.END_TIME BETWEEN t.StartHour AND t.EndHour) 
) a 
GROUP BY t.StartHour 
OPTION (MAXRECURSION 0) 

演示鏈接:http://rextester.com/HFXKL42797

+0

優秀!!,就像一個魅力 我已經取代 ISNULL(END_TIMESTAMP,GETDATE()) 和 ISNULL(分鐘(a.GROUP_ID) 0),以避免空, 非常感謝 – RogerWilco

+0

我給你留下了一個小問題。它可以通過測試找到。提示:min groupid。 :) – TriV