| Author |
Topic |
|
harlingtonthewizard
Constraint Violating Yak Guru
352 Posts |
Posted - 2009-03-26 : 01:27:00
|
| Table called eventdetails as follows:EventID Time EventType SiteID1 2009-03-18 15:00:00 A 12 2009-03-18 15:00:01 B 13 2009-03-18 15:00:02 B 24 2009-03-18 15:00:03 SiteAck 25 2009-03-18 15:00:04 C 26 2009-03-18 15:00:05 C 17 2009-03-18 15:00:06 SiteAck 18 2009-03-18 15:00:07 SiteAck 29 2009-03-18 15:00:08 A 310 2009-03-18 15:00:09 D 111 2009-03-18 15:00:15 SiteAck 1Create Table #e ([EventID] int, [Time] DateTime, [EventType] nvarchar (50), [SiteID] int)Insert into #e ([EventID], [Time], [EventType], [SiteID])Select '1', '2009-03-18 15:00:00', 'A', '1'Union AllSelect '2', '2009-03-18 15:00:01', 'B', '1'Union AllSelect '3', '2009-03-18 15:00:02', 'B', '2'Union AllSelect '4', '2009-03-18 15:00:03', 'SiteAck', '2'Union AllSelect '5', '2009-03-18 15:00:04', 'C', '2'Union AllSelect '6', '2009-03-18 15:00:05', 'C', '1'Union AllSelect '7', '2009-03-18 15:00:06', 'SiteAck', '1'Union AllSelect '8', '2009-03-18 15:00:07', 'SiteAck', '2'Union AllSelect '9', '2009-03-18 15:00:08', 'A', '3'Union AllSelect '10', '2009-03-18 15:00:09', 'D', '1'Union AllSelect '11', '2009-03-18 15:00:15', 'SiteAck', '1'Select * from #eDrop Table #eWhat I need is the response time in seconds between each set for first event and the next siteack event for each site. Basically first event datediff with the first siteack for the respective site. Next set is first event after the first siteack with second siteack for the respective site. Process repeats, events from different sites are interleaved.OutputEventTime SiteAckTime SiteID ResponseTime2009-03-18 15:00:00 2009-03-18 15:00:06 1 62009-03-18 15:00:02 2009-03-18 15:00:03 2 12009-03-18 15:00:04 2009-03-18 15:00:07 2 32009-03-18 15:00:09 2009-03-18 15:00:15 1 6 |
|
|
ayamas
Aged Yak Warrior
552 Posts |
Posted - 2009-03-26 : 03:33:24
|
| I dont know whether this is the best solution but I am getting the desired result.Declare @tbl table(eventid int,time datetime,eventtype varchar(30),siteid int)insert into @tblselect 1, '2009-03-18 15:00:00', 'A', 1 union allselect 2, '2009-03-18 15:00:01' ,'B', 1 union allselect 3, '2009-03-18 15:00:02' ,'B' ,2 union allselect 4, '2009-03-18 15:00:03' ,'SiteAck', 2 union allselect 5, '2009-03-18 15:00:04' ,'C', 2 union allselect 6, '2009-03-18 15:00:05' ,'C', 1 union allselect 7, '2009-03-18 15:00:06' ,'SiteAck', 1 union allselect 8, '2009-03-18 15:00:07' ,'SiteAck', 2 union allselect 9, '2009-03-18 15:00:08' ,'A', 3 union allselect 10, '2009-03-18 15:00:09' ,'D',1 union allselect 11, '2009-03-18 15:00:15' ,'SiteAck', 1select t1.eventid , t1.siteid, t2.time as Eventtime, t1.time as SiteAckTime, t1.eventtype, ROW_NUMBER() over(partition by t1.siteid order by t1.siteid)as rowid into #temp from @tbl t1 cross apply(select eventid,siteid,time,eventtype from @tbl)t2where t1.eventtype='siteAck' and t2.eventtype<>'siteAck'and t1.siteid=t2.siteidSelect Eventtime,SiteAckTime,t1.siteid,DATEDIFF(SECOND,Eventtime,SiteAckTime)as ResponseTime from (select MAX(rowid)as rowid,siteid from #temp group by siteid union select min(rowid)rowid,siteid from #temp group by siteid)t1inner join #temp t2 on t1.rowid=t2.rowid and t1.siteid=t2.siteiddrop table #temp |
 |
|
|
|
|
|