Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Consecutive records prior to current record

Status
Not open for further replies.

crystaldev1

Programmer
Nov 6, 2003
232
US
Hello, I have following dataset and need assistance with query to meet the following requirements:

PAYMENTKEY CUSTOMERKEY DATE PAYMETHOD RESPONSE
1 1 11/1 C A
2 1 1/3 C A
3 1 1/5 C D
4 1 3/6 C A
5 1 3/7 C A
21 2 1/3 C A
25 2 3/2 C A
27 2 3/10 C A
29 3 2/01 C D
31 3 2/02 D A
40 3 2/05 C A

-For each paymentkey, I would like to get a count of consecutive records prior to curent PAYMENTKEY where PAYMETHOD = C and RESPONSE = A.
-Prior records has to be within 60 days from the same customerkey.
-Only interested in consecutive records since the last D (denied) response. So if there was payment with response of D 1 day before, then consecutive record would be 0. Don't care about anything prior to D (if there is a D response).

Results would be following:
Paymentkey ConsecutiveCount
1 0
2 1
3 2
4 0
5 1
21 0
25 1
27 1 (has to be within 60 days)
29 0
31 0
40 1


Thanks!
 
PAYMENTKEY CUSTOMERKEY DATE PAYMETHOD RESPONSE
01[tab] 1[tab] 11/01[tab] C[tab] A
02 [tab]1[tab] 01/03[tab] C[tab] A
03[tab] 1[tab] 01/05[tab] C[tab] D
04[tab] 1[tab] 03/06[tab] C[tab] A
05[tab] 1[tab] 03/07[tab] C[tab] A
21[tab] 2[tab] 01/03[tab] C [tab]A
25[tab] 2[tab] 03/02[tab] C[tab] A
27[tab] 2[tab] 03/10[tab] C[tab] A
29[tab] 3[tab] 02/01[tab] C[tab] D
31[tab] 3[tab] 02/02[tab] D[tab] A
40[tab] 3[tab] 02/05[tab] C[tab] A
 
To align your data use tag [ignore][pre][/ignore]

[pre]
PAYMENTKEY CUSTOMERKEY DATE PAYMETHOD RESPONSE
01 1 11/01 C A
02 1 01/03 C A
03 1 01/05 C D
04 1 03/06 C A
05 1 03/07 C A
21 2 01/03 C A
25 2 03/02 C A
27 2 03/10 C A
29 3 02/01 C D
31 3 02/02 D A
40 3 02/05 C A
[/pre][ignore][/pre][/ignore]

:)

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.
 
You have ordered your data in some sort of sequence. This is not the way that it is stored in Oracle. You have imposed an order on your data, some sort sequence. So, build a CURSOR to read the data, with an ORDER BY clause. Then FETCH each row checking for a D, when you find a D, continue to FETCH records and do your counting until you hit your end condition.

==================================
adaptive uber info galaxies (bigger, better, faster than agile big data clouds)


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top