create table ae_misc_trxs (ae_num number, trx_id number, trx_type varchar2(10), amount number)
Table created.
Insert Data
INSERT ALL
INTO ae_misc_trxs VALUES(123,1, 'INV', +10)
INTO ae_misc_trxs VALUES(123,2, 'PMT', -10)
INTO ae_misc_trxs VALUES(123,3, 'INV',+12)
INTO ae_misc_trxs VALUES(123,4, 'PMT', -10)
INTO ae_misc_trxs VALUES(123,5, 'PMT', -2)
INTO ae_misc_trxs VALUES(123,6, 'PMT', -8)
INTO ae_misc_trxs VALUES(123,7, 'PMT',-3)
INTO ae_misc_trxs VALUES(123,8, 'PMT',-2)
INTO ae_misc_trxs VALUES(123,9, 'INV', +13)
INTO ae_misc_trxs VALUES(123,10, 'INV', +60)
INTO ae_misc_trxs VALUES(123,11, 'PMT', -50)
INTO ae_misc_trxs VALUES(123,12, 'PMT', -5)
INTO ae_misc_trxs VALUES(123,13, 'PMT',-1)
SELECT NULL
FROM DUAL;
/
Match_Recognize_???
SELECT *
FROM ae_misc_trxs A
MATCH_RECOGNIZE(
PARTITION BY ae_num ORDER BY trx_id
MEASURES inv.trx_id AS inv_trx
,pmt.trx_id AS pmt_trx
,MATCH_NUMBER() AS mno
ALL ROWS PER MATCH
AFTER MATCH SKIP TO LAST INV
PATTERN (inv pmt+)
DEFINE INV AS inv.trx_type='INV' AND (inv.amount + NEXT(pmt.amount) = 0)
,PMT AS pmt.trx_type='PMT'
);
I am trying to apply Match_Recognize to find all the payments that make up the invoice. More than one payment can be matched to one Invoice.
e.g. Trx_id 2 matches to trx_id 1
Trx_id 5,4 matches to Trx_id 3
6,7,8 matches to 9
11,12,13 DOES not MATCH to 10.