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)
 AGAIN HELP IN CURSOR PLS

Author  Topic 

Radhiga
Starting Member

35 Posts

Posted - 2006-04-19 : 06:57:58
HAI,
IM EXECUTING MY QUERY WHICH HAS GOT CURSOR TO MANIPULATE..
I GET THE RESULT AS THE FOLLOWING
TIME NAME ACTION
----------- ---------------------- -------------------------
353 Annie ATTEND

(1 row(s) affected)

----------------------- -------------------------
458 Annie ATTEND

(1 row(s) affected)

--------------------------------- -------------------------
34 bk ATTEND

(1 row(s) affected)

--------------------------------- -------------------------
-49 EQ384CP(5316)_(007) ATEND

(1 row(s) affected)

----------- -----------------------------------------
244 Helpdesk ISDN Room 1 ATTEND


ie all different Rows. now i need to sum up the time group by name..
for exmple for annie there are two time values//i need to sum up that and show in one row...how can i do that...
i dont want to use #temp table.

(1 row(s) affected)

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2006-04-19 : 07:11:24
You can use then table variable, insert the all the rows affected in that table and then use Sum function followed by group by clause

If Debugging is the process of removing Bugs then i Guess programming should be process of Adding them.
Go to Top of Page

Radhiga
Starting Member

35 Posts

Posted - 2006-04-19 : 07:15:27
customerid customername Action DateTime
1 john calloperator 4/10/06 10:00
1 john Attend 4/10/06 10:01
1 john Hold 4/10/06 10:05
1 john Attend 4/10/06 10:15
2 jothi calloperator 4/10/06 20:00
2 jothi Attend 4/10/06 20:10

this is my table.
now i want to display the report as the follows

id name Waitingtime date
1 john 11(Sec) 4/10/2006
2 jothi 10 4/10/2006


actully im using cursors to do it..
so now i get the result as

id name Waitingtime date
1 john 1(Sec) 4/10/2006
1 john 10(Sec) 4/10/2006
2 jothi 10 4/10/2006

how can i solve this

this is the actul requirment for which i asked for the help
Go to Top of Page

Krankensteins
Starting Member

24 Posts

Posted - 2006-04-19 : 07:20:55
Try these :
select OrderID,CustomerID
from dbo.Orders
ORDER BY CustomerID
COMPUTE sum(OrderID) by CustomerID


Go to Top of Page

Radhiga
Starting Member

35 Posts

Posted - 2006-04-19 : 07:24:33
sORRY I THAT'S IS NOT TABLE..THIS IS THE OUTPUT OF MY SELECT QUERY IN WHICH I HAVE USED CURSOR
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-04-19 : 08:22:22
[code]
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', '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 in ('CallOperator', 'Hold')
and e.action = 'Attend'
) a
group by cust_id, cust_name
[/code]



KH


Go to Top of Page

Radhiga
Starting Member

35 Posts

Posted - 2006-04-19 : 08:47:46
Thanks a lot for ur effort..i need to verify with my PM am i allowed to use Temp table..Generally they dont encourage us...that's y im trying how can i do this without temp table...
may be i need to do it in the front end or somewhere...im also confused to proceed with this.
quote:
Originally posted by khtan


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', '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 in ('CallOperator', 'Hold')
and e.action = 'Attend'
) a
group by cust_id, cust_name




KH




Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-04-19 : 08:53:07
Radhiga, again I say you always want to use Cursor. If you post some sample data and the result you want, you may get better answer than you expected

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-04-19 : 09:22:40
"Thanks a lot for ur effort..i need to verify with my PM am i allowed to use Temp table..Generally they dont encourage us...that's y im trying how can i do this without temp table..."
I did not use temp table. The table variable is for testing the query



KH


Go to Top of Page

Sarakumar
Posting Yak Master

108 Posts

Posted - 2006-04-19 : 21:35:08
quote:
Originally posted by madhivanan

Radhiga, again I say you always want to use Cursor. If you post some sample data and the result you want, you may get better answer than you expected

Madhivanan

Failing to plan is Planning to fail




YES, I HAVE POSTED MY ACTUAL REQUIRMENT ALREADY.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-04-19 : 21:45:43
quote:
Originally posted by Sarakumar
YES, I HAVE POSTED MY ACTUAL REQUIRMENT ALREADY.


What is your requirement ?



KH


Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-04-20 : 01:32:07
>>YES, I HAVE POSTED MY ACTUAL REQUIRMENT ALREADY.

Are you the Original Poster?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Radhiga
Starting Member

35 Posts

Posted - 2006-04-20 : 09:13:11
Thanks a lot for ur Effort. Sorry to ask u another question in the same doubt...
i need to use this custid to map with another table..but im not able to do...can u guide me for that also...
Thanks a loat


quote:
Originally posted by Radhiga

Thanks a lot for ur effort..i need to verify with my PM am i allowed to use Temp table..Generally they dont encourage us...that's y im trying how can i do this without temp table...
may be i need to do it in the front end or somewhere...im also confused to proceed with this.
quote:
Originally posted by khtan


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', '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 in ('CallOperator', 'Hold')
and e.action = 'Attend'
) a
group by cust_id, cust_name




KH






Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-04-20 : 09:15:56
"i need to use this custid to map with another table..but im not able to do...can u guide me for that also..."
What do you mean ? Can you describe more in details ?



KH


Go to Top of Page

Radhiga
Starting Member

35 Posts

Posted - 2006-04-20 : 09:31:08
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 in ('CallOperator', 'Hold')
and e.action = 'Attend'
) a
group by cust_id, cust_name

i have another table in which i have got customers billing info
custid billing amt
1 john 1000
2 jothy 2000
3 shyam 3000
now as we displayed i wnat to take the billing for the records which we retrieved..ie shyam shouldnt get included in that...so i need to use inner join to map this to two table...im not able to do that...

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-04-20 : 09:40:07

select cust_id, cust_name, total, b.billing_amt
from
(
select cust_id, cust_name, sum(datediff(minute, s_datetime, e_datetime)) as total
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 in ('CallOperator', 'Hold')
and e.action = 'Attend'
) a
group by cust_id, cust_name
) c
inner join billing_info b
on c.cust_id = b.cust_id




KH


Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-04-20 : 10:48:04
Also, use join and dont try to use Cursor

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Radhiga
Starting Member

35 Posts

Posted - 2006-04-20 : 20:25:09
declare @BillingTbl table
(
cust_id int,
cust_name varchar(20),
Amt money
)

insert into @BillingTbl
select 1, 'john', 1000 union all
select 2, 'jothi', 2000 union all
select 3, 'Shyam', 3000


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', '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 b.cust_id, b.cust_name, b.Amt
from
(
select cust_id, cust_name, sum(datediff(minute, s_datetime, e_datetime))as dt
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 in ('CallOperator', 'Hold')
and e.action = 'Attend'
) a
group by cust_id, cust_name
) c
inner join @BillingTbl b
on c.cust_id = b.cust_id




Hello,
The solution which u have me i tried...it is giving me internal sql error...
that's y i asked u how to do this...
any idea...

See the above..
the error msg is Server: Msg 8624, Level 16, State 13, Line 31
Internal SQL Server error.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-04-20 : 21:03:16
"Internal SQL Server error."
Yeah I got that also. Actually i encounter this from time to time when my query gets too complicated and as usual it always near deadline when i need to rush it out when i encounter this and hence don't have the luxury to research into it. My work around is to re-write it differently.

Just did some rearch and find this. http://support.microsoft.com/kb/830466.
quote:
STATUS
Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section of this article.
This problem was first corrected in Microsoft SQL Server 2000 Service Pack 4.


My SQLServer is already SP4 but I still get the error. Anyone else can help ?



KH


Go to Top of Page

Radhiga
Starting Member

35 Posts

Posted - 2006-04-20 : 21:10:53
Can anyone help me in this...im totally confuesed how to do this...
Help pls
Go to Top of Page

Radhiga
Starting Member

35 Posts

Posted - 2006-04-20 : 21:46:16
HAI, CAN U GIVE ME THE IDEA HOW CAN I IMPLEMENT THIS ...OR IS IT POSSIBLE TO USE CURSOR OR SOMETHING ELSE TO FIX THIS..


quote:
Originally posted by khtan

"Internal SQL Server error."
Yeah I got that also. Actually i encounter this from time to time when my query gets too complicated and as usual it always near deadline when i need to rush it out when i encounter this and hence don't have the luxury to research into it. My work around is to re-write it differently.

Just did some rearch and find this. http://support.microsoft.com/kb/830466.
quote:
STATUS
Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section of this article.
This problem was first corrected in Microsoft SQL Server 2000 Service Pack 4.


My SQLServer is already SP4 but I still get the error. Anyone else can help ?



KH




Go to Top of Page
    Next Page

- Advertisement -