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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Urgent help pls in the query

Author  Topic 

Sarakumar
Posting Yak Master

108 Posts

Posted - 2006-04-24 : 00:01:14
Hai i have the similar requirment which was discussed earlier in this forum..using the same sample to aske for the question


declare @table table
(
cust_id int,
cust_name varchar(20),
action varchar(20),
date_time datetime
)

insert into @table
select 1, 'john', 'CallOperator', '2006-04-10 10:00' union all
select 1, 'john', 'Attend', '2006-04-10 10:01' union all
select 1, 'john', 'Hold', '2006-04-10 10:05' union all
select 1, 'john', 'Hold', '2006-04-10 10:08' union all
select 1, 'john', 'Attend', '2006-04-10 10:15' union all
select 2, 'jothi', 'CallOperator', '2006-04-10 20:00' union all
select 2, 'jothi', 'Attend', '2006-04-10 20:10'

select cust_id, cust_name, sum(datediff(minute, s_datetime, e_datetime))
from
(
select s.cust_id, s.cust_name,
s.action as s_action, s.date_time as s_datetime,
e.action as e_action, e.date_time as e_datetime
from @table s inner join @table e
on s.cust_id = e.cust_id
and e.date_time = (select min(date_time) from @table x
where x.cust_id = s.cust_id
and x.action = 'Attend'
and x.date_time > s.date_time)
where (s.action ='CallOperator' or s.action ='Hold')
and e.action = 'Attend'

) a
group by cust_id, cust_name

hai in this i have added one more row...now it is giving me the wrong output
now i need to get the wait time as (first 'CallOperator' time('2006-04-10 10:00') - immediate next attend ('2006-04-10 10:01' ) )+ (second hold + the immediate attend)...

can u pls help me in this

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-04-24 : 01:09:27

declare @table table
(
cust_id int,
cust_name varchar(20),
[action] varchar(20),
date_time datetime
)

insert into @table
select 1, 'john', 'CallOperator', '2006-04-10 10:00' union all
select 1, 'john', 'Attend', '2006-04-10 10:01' union all
select 1, 'john', 'Hold', '2006-04-10 10:05' union all
select 1, 'john', 'Hold', '2006-04-10 10:08' union all
select 1, 'john', 'Attend', '2006-04-10 10:15' union all
select 2, 'jothi', 'CallOperator', '2006-04-10 20:00' union all
select 2, 'jothi', 'Attend', '2006-04-10 20:10'

declare @table2 table
(
cust_id int,
cust_name varchar(20),
s_action varchar(20),
s_datetime datetime,
e_action varchar(20),
e_datetime datetime
)

insert into @table2 (cust_id, cust_name, s_action, s_datetime, e_action, e_datetime)
select cust_id, cust_name, s_action, max(s_datetime) as s_datetime, e_action, max(e_datetime) as e_datetime
from
(
select s.cust_id, s.cust_name,
s.action as s_action, s.date_time as s_datetime,
e.action as e_action, e.date_time as e_datetime
from @table s inner join @table e
on s.cust_id = e.cust_id
and e.date_time = (select min(date_time) from @table x
where x.cust_id = s.cust_id
and x.action = 'Attend'
and x.date_time > s.date_time)
where (s.action ='CallOperator' or s.action ='Hold')
and e.action = 'Attend'
) a
group by cust_id, cust_name, s_action, e_action

select cust_id, cust_name, sum(datediff(minute, s_datetime, e_datetime))
from @table2
group by cust_id, cust_name




KH


Go to Top of Page

Sarakumar
Posting Yak Master

108 Posts

Posted - 2006-04-24 : 02:18:34
Thanks a lot for ur effort. is it very expensive to use table variable
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-04-24 : 02:20:51
not really. Unless you have millions of records. Just try it out and see the result



KH


Go to Top of Page
   

- Advertisement -