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 |
|
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 FOLLOWINGTIME 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 ATTENDie 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 clauseIf Debugging is the process of removing Bugs then i Guess programming should be process of Adding them. |
 |
|
|
Radhiga
Starting Member
35 Posts |
Posted - 2006-04-19 : 07:15:27
|
| customerid customername Action DateTime1 john calloperator 4/10/06 10:001 john Attend 4/10/06 10:011 john Hold 4/10/06 10:051 john Attend 4/10/06 10:152 jothi calloperator 4/10/06 20:002 jothi Attend 4/10/06 20:10this is my table.now i want to display the report as the followsid name Waitingtime date 1 john 11(Sec) 4/10/20062 jothi 10 4/10/2006actully im using cursors to do it..so now i get the result asid name Waitingtime date 1 john 1(Sec) 4/10/20061 john 10(Sec) 4/10/20062 jothi 10 4/10/2006how can i solve thisthis is the actul requirment for which i asked for the help |
 |
|
|
Krankensteins
Starting Member
24 Posts |
Posted - 2006-04-19 : 07:20:55
|
| Try these :select OrderID,CustomerID from dbo.OrdersORDER BY CustomerIDCOMPUTE sum(OrderID) by CustomerID |
 |
|
|
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 |
 |
|
|
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 @tableselect 1, 'john', 'CallOperator', '2006-04-10 10:00' union allselect 1, 'john', 'Attend', '2006-04-10 10:01' union allselect 1, 'john', 'Hold', '2006-04-10 10:05' union allselect 1, 'john', 'Attend', '2006-04-10 10:15' union allselect 2, 'jothi', 'CallOperator', '2006-04-10 20:00' union allselect 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') agroup by cust_id, cust_name[/code] KH |
 |
|
|
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 @tableselect 1, 'john', 'CallOperator', '2006-04-10 10:00' union allselect 1, 'john', 'Attend', '2006-04-10 10:01' union allselect 1, 'john', 'Hold', '2006-04-10 10:05' union allselect 1, 'john', 'Attend', '2006-04-10 10:15' union allselect 2, 'jothi', 'CallOperator', '2006-04-10 20:00' union allselect 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') agroup by cust_id, cust_name KH
|
 |
|
|
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 expectedMadhivananFailing to plan is Planning to fail |
 |
|
|
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 |
 |
|
|
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 expectedMadhivananFailing to plan is Planning to fail
YES, I HAVE POSTED MY ACTUAL REQUIRMENT ALREADY. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-04-19 : 21:45:43
|
quote: Originally posted by SarakumarYES, I HAVE POSTED MY ACTUAL REQUIRMENT ALREADY.
What is your requirement ? KH |
 |
|
|
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?MadhivananFailing to plan is Planning to fail |
 |
|
|
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 loatquote: 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 @tableselect 1, 'john', 'CallOperator', '2006-04-10 10:00' union allselect 1, 'john', 'Attend', '2006-04-10 10:01' union allselect 1, 'john', 'Hold', '2006-04-10 10:05' union allselect 1, 'john', 'Attend', '2006-04-10 10:15' union allselect 2, 'jothi', 'CallOperator', '2006-04-10 20:00' union allselect 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') agroup by cust_id, cust_name KH
|
 |
|
|
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 |
 |
|
|
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') agroup by cust_id, cust_namei have another table in which i have got customers billing infocustid billing amt1 john 10002 jothy 20003 shyam 3000now 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... |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-04-20 : 09:40:07
|
select cust_id, cust_name, total, b.billing_amtfrom(select cust_id, cust_name, sum(datediff(minute, s_datetime, e_datetime)) as totalfrom(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_datetimefrom @table s inner join @table eon s.cust_id = e.cust_idand e.date_time = (select min(date_time) from @table xwhere x.cust_id = s.cust_idand x.action = 'Attend'and x.date_time > s.date_time)where s.action in ('CallOperator', 'Hold')and e.action = 'Attend') agroup by cust_id, cust_name) c inner join billing_info bon c.cust_id = b.cust_id KH |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2006-04-20 : 10:48:04
|
Also, use join and dont try to use Cursor MadhivananFailing to plan is Planning to fail |
 |
|
|
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 @BillingTblselect 1, 'john', 1000 union allselect 2, 'jothi', 2000 union allselect 3, 'Shyam', 3000 declare @table table( cust_id int, cust_name varchar(20), action varchar(20), date_time datetime)insert into @tableselect 1, 'john', 'CallOperator', '2006-04-10 10:00' union allselect 1, 'john', 'Attend', '2006-04-10 10:01' union allselect 1, 'john', 'Hold', '2006-04-10 10:05' union allselect 1, 'john', 'Attend', '2006-04-10 10:15' union allselect 2, 'jothi', 'CallOperator', '2006-04-10 20:00' union allselect 2, 'jothi', 'Attend', '2006-04-10 20:10'select b.cust_id, b.cust_name, b.Amtfrom(select cust_id, cust_name, sum(datediff(minute, s_datetime, e_datetime))as dtfrom( 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') agroup by cust_id, cust_name) c inner join @BillingTbl bon c.cust_id = b.cust_idHello, 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 31Internal SQL Server error. |
 |
|
|
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: STATUSMicrosoft 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 |
 |
|
|
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 |
 |
|
|
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: STATUSMicrosoft 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
|
 |
|
|
Next Page
|
|
|
|
|