I was playing around with capturing wait statistics and was looking at trying to put out some output that would subtract previous capture/name record value from next capture/name record given a date range. I was thinking I should use a CTE and OVER(), but am having trouble thinking of how right now. Anyone feel like giving some direction please?
raiserror('Creating table ''%s''', 0, 1, 'Waits.Collection') create table Waits.Collection (CollectionID int identity (1,1) ,CollectionTime datetime not null ,CreatedOn datetime not null ,CreatedBy nvarchar(128) not null ,constraint pk__Waits__Collection__CollectionID primary key clustered ( CollectionID asc ) with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [PRIMARY] ) on [PRIMARY] go raiserror('Creating table ''%s''', 0, 1, 'Waits.Statistic') create table Waits.Statistic (StatisticID int identity (1,1) ,CollectionID int not null ,WaitType nvarchar(60) not null ,WaitingTaskCount bigint not null ,WaitTimeMS bigint not null ,MaxWaitTimeMS bigint not null ,SignalWaitTimeMS bigint not null ,ResourceWaitTimeMS bigint not null ,CreatedOn datetime not null ,CreatedBy nvarchar(128) not null ,constraint pk__Waits__Statistic__StatisticID primary key clustered ( StatisticID asc ) with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [PRIMARY] ) on [PRIMARY] go alter table Waits.Statistic with check add constraint fk__Waits__Statistic__CollectionID__to__Waits__Collection__CollectionID foreign key(CollectionID) references Waits.Collection (CollectionID) go declare @ii_Rc int ,@idt_StartDate datetime ,@idt_EndDate datetime set @idt_StartDate = (getdate() - 30) set @idt_EndDate = (getdate()) ;with cte as (select c.CollectionID ,c.CollectionTime ,s.WaitType ,s.WaitingTaskCount ,s.WaitTimeMS ,s.ResourceWaitTimeMS ,s.SignalWaitTimeMS from Waits.Statistic s inner join Waits.Collection c on s.CollectionID = c.CollectionID where c.CollectionTime between @idt_StartDate and @idt_EndDate) select r2.WaitType ,(r2.WaitingTaskCount - r1.WaitingTaskCount) as WaitingTaskCount ,((r2.WaitTimeMS - r1.WaitTimeMS) / 1000.0) as WaitTimeS ,((r2.ResourceWaitTimeMS - r1.ResourceWaitTimeMS) / 1000.0) as ResourceWaitTimeS ,((r2.SignalWaitTimeMS - r1.SignalWaitTimeMS) / 1000.0) as SignalWaitTimeS from (select s.CollectionID ,s.CollectionTime ,s.WaitType ,s.WaitingTaskCount ,s.WaitTimeMS ,s.ResourceWaitTimeMS ,s.SignalWaitTimeMS from cte s) as r1 inner join (select s.CollectionID ,s.CollectionTime ,s.WaitType ,s.WaitingTaskCount ,s.WaitTimeMS ,s.ResourceWaitTimeMS ,s.SignalWaitTimeMS from cte s) as r2 on r1.WaitType = r2.WaitType and r2.CollectionID = (select MIN(c.CollectionID) from cte c where c.CollectionID > r1.CollectionID and c.WaitType = r1.WaitType)
John M. Couch