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 |
|
00kevin
Yak Posting Veteran
78 Posts |
Posted - 2006-01-03 : 11:14:58
|
| is there a way to speed this up and get rid of the cursor? declare @callee_id intDECLARE cur CURSOR FOR select callee_id from PRINT_LOG where request_id = @idOPEN curFETCH NEXT FROM cur INTO @callee_id WHILE @@FETCH_STATUS = 0BEGINinsert into #TEMP_CALL_SHEETS ( callee_id , branch_id , bucket_id , ,bucket_id ) SELECT TOP 10 callee_id ,branch_id ,bucket_id ,branch FROM vwCallSheet where callee_id = @callee_id order by completed_date descFETCH NEXT FROM cur INTO @callee_idENDCLOSE curDEALLOCATE curselect * from #TEMP_CALL_SHEETSdrop table #temp_call_sheets |
|
|
Kristen
Test
22859 Posts |
Posted - 2006-01-03 : 11:23:20
|
Dunno if this would work (not sure if PL.callee_id is in scope in the function call)CREATE FUNCTION MyFunction(@callee_id int)RETURNS TABLEAS RETURN SELECT TOP 10 callee_id ,branch_id ,bucket_id ,branch FROM vwCallSheet WHERE callee_id = @callee_id ORDER BY completed_date DESCGOINSERT INTO #TEMP_CALL_SHEETS(callee_id , branch_id , bucket_id ,bucket_id)SELECT CS.*FROM PRINT_LOG AS PL JOIN dbo.MyFunction(PL.callee_id) AS CSWHERE PL.request_id = @id Kristen |
 |
|
|
00kevin
Yak Posting Veteran
78 Posts |
Posted - 2006-01-03 : 11:40:25
|
| well that is a good idea but it doesn't like (PL.callee_id)Incorrect syntax near '.'. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
Kristen
Test
22859 Posts |
Posted - 2006-01-04 : 07:10:04
|
| "it doesn't like (PL.callee_id)"I was half expecting that. Bother!Kristen |
 |
|
|
mmarovic
Aged Yak Warrior
518 Posts |
Posted - 2006-01-05 : 09:47:44
|
| What combination of columns would be unique row identifier for vwCallSheet? |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2006-01-05 : 10:14:02
|
This solution uses a temp table with an IDENTITY column to create a row sequence. After you have the row sequence, the rest is easy.create table #TEMP_CALL_SHEETS(-- Sequence is to preserve order-- by completed_date within callee_idSequence int not null identity(1,1) primary key clustered,callee_id int not null,branch_id int not null,bucket_id int not null,branch int not null,completed_date datetime not null)insert into #TEMP_CALL_SHEETS ( callee_id, branch_id, bucket_id, branch, completed_date )SELECT -- top used to ensure correct sequence top 100 percent callee_id, branch_id, bucket_id, branch, completed_dateFROM vwCallSheetorder by callee_id, completed_date descselect a.callee_id, a.branch_id, a.bucket_id, a.branchfrom #TEMP_CALL_SHEETS a join ( select bb.callee_id -- last possible sequence to get 10 or less Last_Sequence = min(bb.Sequence)+9 from #TEMP_CALL_SHEETS bb group by bb.CALLID ) b on a.callee_id = b.callee_id and a.Sequence <= b.Last_Sequenceorder by a.callee_id, a.Sequence CODO ERGO SUM |
 |
|
|
ehorn
Master Smack Fu Yak Hacker
1632 Posts |
Posted - 2006-01-05 : 19:42:50
|
How about something like this:--create tally table to insure proper join on callee_idcreate table dbo.numbers ( n int primary key)insert dbo.numbersselect 1 union all select 2 union allselect 3 union all select 4 union allselect 5 union all select 6 union allselect 7 union all select 8 union allselect 9 union all select 10 -- as many as you might needgoselect v.*from( select top 100 percent n.n, v.callee_id, v.branch_id, v.bucket_id, v.branch, v.completed_date from vwCallSheet v, numbers n group by n.n, v.callee_id, v.branch_id, v.bucket_id, v.branch having n.n <= count(o.orderid) and n.n <= 10 order by completed_date desc) vjoin PRINT_LOG o on o.callee_id = v.callee_id and o.request_id = @idorder by v.callee_id, v.completed_date desc |
 |
|
|
|
|
|
|
|