Calculating difference between row values

# Thread: Calculating difference between row values

1. Junior Member
Join Date
Dec 1969
Posts
21

## Calculating difference between row values

I have a table with CustId and RejectDate columns eg.<BR>CustId RejectDate<BR>001 05/31/2001<BR>001 06/30/2001<BR>001 07/31/2001<BR>002 05/31/2001<BR>002 08/31/2001<BR><BR>I need to see only those custId&#039;s where the difference between the RejectDate is consecutive (ie the difference is a month)<BR>in my example that would be CustId 001 , and not 002 <BR>Is there a way to do it in sql? - ps I am using an Oracle database<BR>

2. Join Date
Dec 1969
Posts
86

## RE: Calculating difference between row values

You&#039;ll probably need to use cursors and local variables. The general algorithm would be to set a local variable to the first date then check roll the cursor forward, checking the next date against the value of the local variable. After you&#039;ve done whatever action is necessary (eg updating a flag variable in the table), set the local variable to the date value of the second variable and so forth.<BR>And, no, I&#039;m not going to write out the whole code. ;-)

3. Junior Member
Join Date
Dec 1969
Posts
21

## RE: Calculating difference between row values

thx, but I am trying to avoid using cursors ( to much overhead), was hoping for a select statement using derived columns or corrolated subqueries<BR>I have a choice of doing this in either asp (preferred) or vb.

4. Join Date
Dec 1969
Posts
86

## RE: Calculating difference between row values

If you&#039;re using Oracle, it shouldn&#039;t be too much of a problem. Unless you&#039;ve already tried it or are philosophically against cursors (not an unusual thing for SQL Server DBAs), I would go for cursors and see if you get any serious performance issues.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•