2012-11-02 107 views
0

我解決了1天計算的問題,但是當我將日期分成1天時出現了問題。TSQL日期範圍日復一日地計算

declare @Start datetime 
    declare @Finish datetime 
    declare @TimeRange int 

    set @Start = N'2012-10-16 00:00:00.000' 
    set @Finish = N'2012-10-19 00:00:00.000' 
    set @TimeRange = 1 

    declare @TimeRanges as TABLE (SessionStart datetime, SessionEnd datetime); 

    with TimeRanges as 
    ( select @Start as StartTime, DATEADD(day, @TimeRange, @Start) as EndTime 
     union all 
     select DATEADD(day, @TimeRange, StartTime), DATEADD(day, @TimeRange, EndTime) 
     from TimeRanges 
     where EndTime < @Finish 
    ) 

timeRanges tables將@Start和@Finish分成幾天。

這裏的問題開始什麼:

select StartTime, EndTime,TMP.NoOfCalls  
    from (SELECT TOP 1 DATEADD(SECOND, Number, c.SessionStartTime) CallTime, COUNT(C.ScenarioID) NoOfCalls FROM test c 
INNER JOIN Numbers N ON N.Number <= DATEDIFF(SECOND, C.SessionStartTime, C.SessionCloseTime) 
where c.SessionStarttime >= @Start and c.SessionCloseTime <= @Finish 
GROUP BY DATEADD(SECOND, Number, c.SessionStartTime) 
ORDER BY NoOfCalls DESC 
)as TMP left outer join TimeRanges as TR on @Start <= TR.StartTime and TR.EndTime <= @Finish 
     group by TR.StartTime, TR.EndTime,TMP.NoOfCalls 
     order by TR.StartTime 

這是我得到的結果是:

enter image description here

其實260是N'2012-10-17 00之間的結果:00:00.000'和N'2012-10-18 00:00:00.000',但我希望分開結果。

我的樣品測試表:

SessionID SessionStartTime    SessionCloseTime 
24  2012-10-16 01:00:06.000   2012-10-16 01:01:22.000 
24  2012-10-16 01:00:08.000   2012-10-16 01:01:10.000 
24  2012-10-16 01:00:16.000   2012-10-16 01:01:12.000 
24  2012-10-16 01:00:30.000   2012-10-16 01:01:48.000 
24  2012-10-16 01:00:41.000   2012-10-16 01:02:08.000 
24  2012-10-16 01:00:48.000   2012-10-16 01:01:34.000 
24  2012-10-16 01:00:56.000   2012-10-16 01:03:09.000 
24  2012-10-16 01:01:02.000   2012-10-16 01:02:13.000 
24  2012-10-16 01:01:05.000   2012-10-16 01:03:16.000 
24  2012-10-16 01:01:09.000   2012-10-16 01:02:42.000 
24  2012-10-16 01:01:15.000   2012-10-16 01:02:48.000 
24  2012-10-16 01:01:18.000   2012-10-16 01:02:14.000 
24  2012-10-16 01:01:18.000   2012-10-16 01:02:06.000 
24  2012-10-16 01:01:42.000   2012-10-16 01:03:16.000 
24  2012-10-16 01:01:45.000   2012-10-16 01:03:04.000 
+0

'SELECT'你沒有使用'TimeRanges' CTE前面定義。另外,什麼是數字表格? – GolfWolf

+0

對不起,我糾正它數字只是我在計數時使用的示例數字表開始1,2,3 .... 100000 – cihata87

+0

我仍然試圖找出你想用這個來實現什麼。我在http://www.sqlfiddle.com/#!3/4970c/11上製作了在線運行版本。你可以看看並描述在這種情況下期望的輸出是什麼? – GolfWolf

回答

0

你的問題是左連接,但您的查詢似乎在複雜。

嘗試

with TimeRanges as 
    ( select @Start as StartTime, DATEADD(day, @TimeRange, @Start) as EndTime 
     union all 
     select DATEADD(day, @TimeRange, StartTime), DATEADD(day, @TimeRange, EndTime) 
     from TimeRanges 
     where EndTime < @Finish 
    ) 
     select TimeRanges.StartTime, TimeRanges.EndTime, COUNT(*) 
     from TimeRanges 
      inner join YourTable Sess 
       on sess.sessionstarttime>TimeRanges.StartTime 
       and sess.sessionclosetime< TimeRanges.EndTime 
     group by TimeRanges.StartTime, TimeRanges.EndTime 
在你的問題
+0

仍然每天顯示260 – cihata87