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)
 Cursor Alternative

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-04-01 : 09:14:40
shine writes "This cursor is killing my computer. I have a Claim table which contains ClientsID, ProductID, and ClaimDate. The table has over 10 million claims. I need to identify clients who have submitted more then 14 claims within a 180 day period. I used a cursor to resolve this problem but it takes days to run. Can you suggest an alternative.

I am trying to identify and insert clients who exceed 14 claims into a table. Thanks for your help.

My sql looks something like this:

________________________________________________________________
-- Declare the variables to store the values returned by FETCH.
DECLARE @ClientID char(14),
@ClaimDate datetime,
@Product char(5),
@Product_count int,
@ClientID_cur char(14),
@ClaimDate_cur datetime,
@exist_ClientID char(14)

set @ClientID = 'start'
set @Product_count = 0

DECLARE Claim_cursor CURSOR FOR
--get unique ClientIDs for 2001
select cc.ClientID,
cc.ClaimDate
from ClaimCandidate2001 cc
where cc.ClientID not in (select ClientID from ClaimClientIDsExceed)
order by mc.ClientID

OPEN Claim_cursor

-- Perform the first fetch and store the values in variables.
-- Note: The variables are in the same order as the columns
-- in the SELECT statement.

FETCH NEXT FROM Claim_cursor
INTO @ClientID_cur, @ClaimDate_cur

-- Check @@FETCH_STATUS to see if there are any more rows to fetch.
WHILE @@FETCH_STATUS = 0
BEGIN
--if 14 unique Product exceeded in 180 days then check to skip to next next record
if @ClientID_cur <> @ClientID or @Product_count < 15
begin
--reset variables
set @Product_count = 0
set @ClientID = @ClientID_cur
set @ClaimDate = @ClaimDate_cur

--Get count of unique Product for 180 day period
select @Product_count = count(distinct pc.Product)
from ClaimCandidate2001 pc
where pc.ClientID = @ClientID
and pc.ClaimDate between @ClaimDate and dateadd(d,180,@ClaimDate)
--check to see if unique Product exceed 14
if @Product_count > 14
begin
set @exist_ClientID = 'reset'
--check to see if the ClientID already exists
select @exist_ClientID = isnull(ClientID, 'no')
from ClaimClientIDsExceed
where ClientID like @ClientID

if @exist_ClientID in ('no', 'reset')
begin
--put into list of ClientIDs that exceed 14 for 180 day period
insert into ClaimClientIDsExceed
(ClientID)
values(@ClientID)
end
end
end

-- This is executed as long as the previous fetch succeeds.
FETCH NEXT FROM Claim_cursor
INTO @ClientID_cur, @ClaimDate_cur--, @Product
END

CLOSE Claim_cursor
DEALLOCATE Claim_cursor"

setbasedisthetruepath
Used SQL Salesman

992 Posts

Posted - 2002-04-01 : 09:39:07
it's much better to post your table definitions ( DDL ) rather than code, since your readers will have to dig through it to infer the table structures and the code itself is not altogether relevant.

quote:

I am trying to identify and insert clients who exceed 14 claims into a table.



let's say i have a rowset with a clientID and a claimID ( however constructed ). then your query would be:

select clientID
from {rowset}
group by clientID
having count( distinct claimID ) > 14

setBasedIsTheTruepath
<O>
Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2002-04-02 : 06:18:02
provided your search is based on 180 days since the 1st claim by a client....then a form of the following should work faster....

this won't cover all cases though if the concentration of claims comes after the 2nd claim.


select b.clientid, count(*)
from ClaimCandidate2001 b
inner join
(select clientid, min(ClaimDate) as minrec, min(ClaimDate) + 180 as maxrec
from ClaimCandidate2001 a
group by clientid) as a
on b.ClaimDate >= a.minrec and b.recorddate <= a.maxrec and a.clientid = b.clientid -- and b.clientid in (xxxx,yyy,zzzz)
group by b.clientid
having count(*) > 14



try the code with a smaller data set (or the same dataset with certain known ClientId's selected for testing)

Go to Top of Page
   

- Advertisement -