2017-04-11 90 views
1

希望有人可以協助如何修改以下SQL以實現所需輸出中顯示的結果。我不熟悉TSQL,但知道足夠了解這一點。
我的目標是將我的事件數據和組計爲從午夜開始的15分鐘時間段,但在特定時間段中沒有事件數據時也包括零(0)。將事件分組分成5分鐘的時間段

CURENT查詢#

;With cte As 
(SELECT CONVERT (varchar(5),DATEADD(minute, 15 * 
    (DATEDIFF(minute, '20000101', I.CreateTimestamp)/15), '20000101'),108) 
    AS CreationTime, I.IncidentShortReference AS Ref 
FROM Incident I 
WHERE i.CreateTimestamp between DATEADD(d,-1,GETDATE()) and GETDATE() 
) 
SELECT CTE.CreationTime, count(CTE.Ref) As Count 
FROM cte CTE 
GROUP BY CTE.CreationTime 
ORDER BY CTE.CreationTime 

我的結果

CreationTime count 
00:15   2 
01:00   1 
01:15   1 
01:30   1 
01:45   2 
02:00   1 
02:15   1 
02:30   4 
(Truncated) 

所需的輸出

CreationTime count 
00:15   2 
00:30   0 
00:45   0 
01:00   1 
01:15   1 
01:30   1 
01:45   2 
02:00   1 
02:15   1 
02:30   4 
02:45   0 
03:00   0 
(Truncated) 
+0

包括樣本數據和期望的輸出 –

+2

您的數據只在同一天?如果不是,那麼在2天內同一時間呢? – TriV

+0

即使計數我不明白它來自哪裏..這只是一個粗略的結果 –

回答

1

這在每個範圍與樣本數據使用CTE創造了昨天的每時間戳午夜之間,現在的記錄,具有計數事件的數量:

從隨機間隔
declare @incident table (CreateTimestamp datetime, IncidentShortReference varchar(5)) 
insert into @incident values ('4/10/2017 11:11:00', 'test') 
insert into @incident values ('4/10/2017 11:12:00', 'test') 
insert into @incident values ('4/10/2017 11:21:00', 'test') 
insert into @incident values ('4/10/2017 11:31:00', 'test') 
insert into @incident values ('4/10/2017 13:31:00', 'test') 

DECLARE @dt datetime 
SELECT @dt = dateadd(d, datediff(d, 0, getdate()), 0) - 1 -- yesterday at midnight 

;with cte as 
(
select @dt dt 
union all 
select DATEADD(minute, 15, dt) as Next15 
FROM cte 
WHERE DATEADD(minute, 15, dt) < GETDATE() 
) 
select convert(varchar(5), dt, 108) as CreationTime, (select count(*) FROM @incident WHERE CreateTimestamp >= dt and CreateTimestamp < dateadd(mi, 15, dt)) as count 
from cte 

輸出樣本:

table

+0

這完全按照需要工作。 我需要包含 選項(maxrecursion 0)來防止Maxiumum遞歸錯誤 – cramar

1

您可以創建一個時間間隔CTE表像this

WITH TIME_CTE 
AS(
    SELECT 
     CAST('20170411 00:15:00' AS DATETIME) AS TimePeriod 
    UNION ALL 
    SELECT 
     DATEADD(MINUTE, 15, TimePeriod) 
    FROM TIME_CTE 
    WHERE 
     DATEADD(MINUTE, 15, TimePeriod) < CAST('20170411 23:59:00' AS DATETIME) 
) 

SELECT 
    LEFT(CONVERT(VARCHAR(10),TimePeriod,108), 5) 
FROM TIME_CTE 

然後用你的原始查詢加入它

WITH TIME_CTE 
AS(
    SELECT 
     CAST('20170411 00:15:00' AS DATETIME) AS TimePeriod 
    UNION ALL 
    SELECT 
     DATEADD(MINUTE, 15, TimePeriod) 
    FROM TIME_CTE 
    WHERE 
     DATEADD(MINUTE, 15, TimePeriod) < CAST('20170411 23:59:00' AS DATETIME) 
), 
CTE 
AS (
SELECT CONVERT (varchar(5),DATEADD(minute, 15 * 
    (DATEDIFF(minute, '20000101', I.CreateTimestamp)/15), '20000101'),108) 
    AS CreationTime, I.IncidentShortReference AS Ref 
FROM Incident I 
WHERE i.CreateTimestamp between DATEADD(d,-1,GETDATE()) and GETDATE() 
) 

SELECT TIME_CTE.TimePeriod, SUM(IIF(CTE.Ref IS NULL, 0, 1)) As Count 
FROM TIME_CTE 
    LEFT JOIN CTE ON CTE.CreationTime = TIME_CTE.TimePeriod 
GROUP BY TIME_CTE.TimePeriod 
ORDER BY TIME_CTE.TimePeriod