也許我已經過簡化您的要求,但考慮到以下
Declare @YourTable table (ID int,Date datetime)
Insert Into @YourTable values
(1,'2016-01-29 10:27:25.603'),
(2,'2016-01-29 10:27:25.620'),
(3,'2016-01-29 10:27:25.637'),
(4,'2016-01-29 10:27:25.653'),
(5,'2016-01-29 10:27:25.723'),
(6,'2016-01-29 10:27:34.647'),
(7,'2016-01-29 10:27:34.667'),
(8,'2016-01-29 10:27:34.680'),
(9,'2016-01-29 10:27:34.690'),
(10,'2016-01-29 10:27:34.707')
Declare @BatchSecondsGap int = 2 -- Seconds Between Batches
Declare @MinObservations int = 5 -- Batch must n or greater
;with cte as (
Select *,Cnt = sum(1) over (Partition By Batch)
From (
Select *,Batch = sum(Flg) over (Order By Date)
From (
Select ID,Date
,Flg = case when DateDiff(SECOND,Lag(Date,1,null) over (Order By Date),Date)>[email protected] then 1 else 0 end
,MS = case when DateDiff(SECOND,Lag(Date,1,Date) over (Order By Date),Date)>[email protected] then 0 else DateDiff(MILLISECOND,Lag(Date,1,Date) over (Order By Date),Date) end
From @YourTable
) A
) B
)
Select Title = 'Total'
,DateR1 = min(Date)
,DateR2 = max(Date)
,BatchCnt = count(Distinct Batch)
,TransCnt = count(*)
,MS_Ttl = sum(MS)
,MS_Avg = avg(MS*1.0)
,MS_Std = stdev(MS)
From cte
Where Cnt>[email protected]
Union All
Select Title = concat('Batch ',Batch)
,DateR1 = min(Date)
,DateR2 = max(Date)
,BatchCnt = count(Distinct Batch)
,TransCnt = count(*)
,MS_Ttl = sum(MS)
,MS_Avg = avg(MS*1.0)
,MS_Std = stdev(MS)
From cte
Where Cnt>[email protected]
Group By Batch
返回
下面的圖片說明,你不會受到懲罰批次之間的時間,所以那麼變得用於最終結果的簡單聚合
我覺得這是完美的開始,仍然需要圍繞SQL代碼。添加了'HAVING count(*)> 10'來僅包含統計顯着的數據,但我的改變似乎在統計學上並不顯着。 :)看起來,該系統有好幾天表現出色,並且表現糟糕,平均處理速度相差20倍。所以計算一個總的平均值並沒有什麼意義。總AVG = 91.21ms,SD = 193.52ms。我將添加日期範圍以嘗試更多地瞭解這一點。很好的回答,接受... – Neolisk
@Neilisk高興地幫助。只要ping通我,如果你需要任何幫助 –
我看到那裏發生了什麼,即使我添加了一個HAVING子句,它隻影響批次的數量並沒有影響「總」的指標。你可以調整你的SQL,以便「總計」只跳過其中只有幾個項目的批次? – Neolisk