we have below tables
create table bca(id number(2));
insert into bca(10);
insert into bca(11);
insert into bca(7);
create TABLE abc(
di NUMBER(2),
fk_id NUMBER(3),
yek VARCHAR2(20),
elv VARCHAR(15)
)
Insert into ABC (DI,FK_ID,YEK,ELV) values (1,10,'DATE','20190320');
Insert into ABC (DI,FK_ID,YEK,ELV) values (2,10,'EXECUTED','Y');
Insert into ABC (DI,FK_ID,YEK,ELV) values (3,11,'DATE','20190320');
Insert into ABC (DI,FK_ID,YEK,ELV) values (4,11,'EXECUTED','N');
Insert into ABC (DI,FK_ID,YEK,ELV) values (5,7,'DATE','20190321');
Insert into ABC (DI,FK_ID,YEK,ELV) values (6,7,'EXECUTED','Y');
I have tried with below query
( SELECT
id ,di,fk_id,yek,elv
FROM
bca
INNER JOIN abc
ON(id=fk_id)
WHERE
-- fk_cmcm_component = 'IRC' AND cmmu_task_type = 'F'
(YEK,ELV)IN(('DATE','20190320'),('EXECUTED','Y')))
below results for above query
10 1 10 DATE 20190320
10 2 10 EXECUTED Y
11 3 11 DATE 20190320
7 6 7 EXECUTED Y
But I required only fk_id 10 results because I am passing 20190320 for YEK and 'Y' for ELV.
required result is
10 1 10 DATE 20190320
10 2 10 EXECUTED Y
Please help me.
That's because your query returns any row where either
yek = 'DATE' and elv = '20190320'
OR
yek = 'EXECUTED' and elv = 'Y'
Is true.
You need to find FK_IDs that have both of these rows. This is a problem for pattern matching!
The pattern you want is a DATE row, followed by EXECUTED.
So you need to define pattern variables for each of these. This checks the YEK & ELV columns have the values you're looking for.
It's per FK_ID, so you need to partition by this. And sort by a column which defines the order they were inserted (DI here).
Which gives:
select * from abc
match_recognize (
partition by fk_id
order by di
all rows per match
pattern ( dt exe )
define
dt as yek = 'DATE'
and elv = '20190320',
exe as yek = 'EXECUTED'
and elv = 'Y'
);
FK_ID DI YEK ELV
10 1 DATE 20190320
10 2 EXECUTED Y