Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
panthagani
Yak Posting Veteran
58 Posts |
Posted - 2006-08-27 : 04:15:36
|
Here is the sample datanType dCDServerTime0 2006-05-12 03:50:46.7701 2006-05-12 04:00:59.15396 2006-05-12 04:01:23.82796 2006-05-12 04:16:01.29714 2006-05-15 05:02:46.85720 2006-06-23 03:23:36.970I am trying to find the time diff between dCDServerTime of the "first" ntype = 1 and the "first" ntype = 96. There can be any number of rows with 1 and 96 but rows with 1 always come before those with 96.I tried by putting a subset of the above result into a table variable but don't seem to get ahead without declaring more variables.declare @t table(Type int, ServerTime datetime)insert into @t select top 1 nType,dCDServerTime from tblEvent where nGlobalCaseID = 11901643 and nType = 1UNIONselect top 1 nType,dCDServerTime from tblEvent where nGlobalCaseID = 11901643 and nType = 96 select * from @t Is it possible to do it with a single select? |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-08-27 : 04:53:19
|
is this what you want ?declare @table table( nType int, dCDServerTime datetime)insert into @tableselect 0, '2006-05-12 03:50:46.770' union allselect 1, '2006-05-12 04:00:59.153' union allselect 96, '2006-05-12 04:01:23.827' union allselect 96, '2006-05-12 04:16:01.297' union allselect 14, '2006-05-15 05:02:46.857' union allselect 20, '2006-06-23 03:23:36.970'select nType, dCDServerTime, datediff(minute, dCDServerTime_1, dCDServerTime), datediff(minute, dCDServerTime_96, dCDServerTime)from( select nType, dCDServerTime, dCDServerTime_1 = (select max(dCDServerTime) from @table x where x.nType = 1 and x.dCDServerTime <= t.dCDServerTime), dCDServerTime_96 = (select max(dCDServerTime) from @table x where x.nType = 96 and x.dCDServerTime <= t.dCDServerTime) from @table t) a KH |
|
|
panthagani
Yak Posting Veteran
58 Posts |
Posted - 2006-08-27 : 05:07:50
|
Thanks a ton! I just need to find the time diff only between the "first" 1 and "first" 96, though. |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-08-27 : 05:18:55
|
sorry. I must had misread it.declare @table table( nType int, dCDServerTime datetime)insert into @tableselect 0, '2006-05-12 03:50:46.770' union allselect 1, '2006-05-12 04:00:59.153' union allselect 96, '2006-05-12 04:01:23.827' union allselect 96, '2006-05-12 04:16:01.297' union allselect 14, '2006-05-15 05:02:46.857' union allselect 20, '2006-06-23 03:23:36.970'select datediff(second, min(n1.dCDServerTime), min(n96.dCDServerTime)) as [Time Diff of first 1 and 96 in secs]from @table as n1, @table as n96where n1.nType = 1and n96.nType = 96 KH |
|
|
panthagani
Yak Posting Veteran
58 Posts |
Posted - 2006-08-27 : 08:59:28
|
Np,Thanks. A small modification needed.. Posting some sample results. Actually, I have around 50,000 records.nType nGlobalCaseID dCDServerTime1 9188963 2006-05-07 23:21:09.26096 9188963 2006-05-07 23:43:08.2301 9419947 2006-05-07 19:21:27.5801 9440738 2006-05-07 23:37:47.6901 9440738 2006-05-07 23:58:54.8001 9440739 2006-05-07 23:22:11.10396 9440739 2006-05-07 23:22:15.7601 9570167 2006-05-07 23:48:25.7401 10084421 2006-05-04 16:17:13.78796 10084421 2006-05-04 16:18:46.7431 10084422 2006-05-04 16:36:15.473Now, I need to do the original calculation (time diff between "1" and "96" for each pair of values in nGlobalCaseID. Eg: nGlobalCaseID = 9188963,9440739,10084421 etc. Only the pairs with (1,96) for a nGlobalCaseID need to be considered. All other single "1"'s and single "96"'s (and multilple 1 or 96 for same nglobalcaseID)can be ignored.So, the result would look lsomething like nGlobalCaseID Timediff(between 1 and 96)-------------- -----------------------I am trying to extract the (1,96) pairs after inserting a subset of original data into a temp table, like this..select nType,nGlobalCaseID,dCDServerTime into #FirstResp from tblEvent wherenGlobalCaseID is not null and nType is not NULL and tByRule is NULL and nType in (1,96)and dCDServerTime > '2006-05-01 00:00:00.000' and dCDServerTime < '2006-06-01 00:00:00.000'select <what goes in here?> from #FirstRespdrop table #FirstRespAgain, thanks for your valuable inputs |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-08-27 : 09:23:51
|
[code]declare @table table( nType int, nGlobalCaseID int, dCDServerTime datetime)insert into @tableselect 1, 9188963, '2006-05-07 23:21:09.260' union allselect 96, 9188963, '2006-05-07 23:43:08.230' union allselect 1, 9419947, '2006-05-07 19:21:27.580' union allselect 1, 9440738, '2006-05-07 23:37:47.690' union allselect 1, 9440738, '2006-05-07 23:58:54.800' union allselect 1, 9440739, '2006-05-07 23:22:11.103' union allselect 96, 9440739, '2006-05-07 23:22:15.760' union allselect 1, 9570167, '2006-05-07 23:48:25.740' union allselect 1, 10084421, '2006-05-04 16:17:13.787' union allselect 96, 10084421, '2006-05-04 16:18:46.743' union allselect 1, 10084422, '2006-05-04 16:36:15.473'select nGlobalCaseID, datediff(second, nType1, nType96) as [TimeDiff in Seconds]from( select nGlobalCaseID, max(case when nType = 1 then dCDServerTime end) as nType1, max(case when nType = 96 then dCDServerTime end) as nType96 from @table group by nGlobalCaseID) awhere nType1 is not nulland nType96 is not null/*nGlobalCaseID TimeDiff in Seconds ------------- ------------------- 9188963 13199440739 410084421 93*/[/code] KH |
|
|
panthagani
Yak Posting Veteran
58 Posts |
Posted - 2006-08-27 : 09:40:58
|
Works like a charm,thanks. The use of case in max function was interesting and informative! |
|
|
panthagani
Yak Posting Veteran
58 Posts |
Posted - 2006-08-27 : 23:05:01
|
khtan,did a small modification. Since I need to calculate the time diff between the first "1" and first "96" in a list of 1's and 96's for a 'nGlobalCaseID',I did the following (changed the max to min). Here is the entire query.select nGlobalCaseID, datediff(second, nType1, nType96) as [First Response Time]from( select nGlobalCaseID, min(case when nType = 1 then dCDServerTime end) as nType1, min(case when nType = 96 then dCDServerTime end) as nType96from tblEvent with (nolock) where nGlobalCaseID is not null and nType is not NULL and tByRule is NULL and nType in (1,96)and dCDServerTime > '2006-05-01 00:00:00.000' and dCDServerTime < '2006-06-01 00:00:00.000'group by nGlobalCaseID ) a where nType1 is not nulland nType96 is not nullThanks. |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-08-27 : 23:24:59
|
thanks for posting back KH |
|
|
|
|
|
|
|