create table T1 (OR_ID integer, CD_ID integer)
Table created.
insert into T1 (OR_ID, CD_ID) values (1, 1)
1 row(s) inserted.
insert into T1 (OR_ID, CD_ID) values (1, 2)
1 row(s) inserted.
insert into T1 (OR_ID, CD_ID) values (2, 11)
1 row(s) inserted.
insert into T1 (OR_ID, CD_ID) values (2, 12)
1 row(s) inserted.
commit
Statement processed.
create table T2 (CD_ID2 integer, R_ID integer)
Table created.
insert into T2 (CD_ID2, R_ID) values (1, 101)
1 row(s) inserted.
insert into T2 (CD_ID2, R_ID) values (2, 102)
1 row(s) inserted.
insert into T2 (CD_ID2, R_ID) values (11, 211)
1 row(s) inserted.
insert into T2 (CD_ID2, R_ID) values (12, 212)
1 row(s) inserted.
commit
Statement processed.
create table T3 (L_ID integer, R_ID2 integer)
Table created.
insert into T3 (L_ID, R_ID2) values (1001, 101)
1 row(s) inserted.
insert into T3 (L_ID, R_ID2) values (1001, 102)
1 row(s) inserted.
insert into T3 (L_ID, R_ID2) values (1002, 101)
1 row(s) inserted.
insert into T3 (L_ID, R_ID2) values (1002, 211)
1 row(s) inserted.
insert into T3 (L_ID, R_ID2) values (1003, 211)
1 row(s) inserted.
insert into T3 (L_ID, R_ID2) values (1003, 212)
1 row(s) inserted.
insert into T3 (L_ID, R_ID2) values (1004, 102)
1 row(s) inserted.
insert into T3 (L_ID, R_ID2) values (1004, 212)
1 row(s) inserted.
insert into T3 (L_ID, R_ID2) values (1004, 101)
1 row(s) inserted.
insert into T3 (L_ID, R_ID2) values (1004, 300)
1 row(s) inserted.
commit
Statement processed.
with o as (
select T1.OR_ID, T1.CD_ID, T2.R_ID,
count(*) over (partition by T1.OR_ID) as CNT
from T1
join T2
on T2.CD_ID2 = T1.CD_ID
),
x as (
select o.OR_ID, o.CNT, T3.L_ID, count(*) as CNT_T3
from o
join T3
on T3.R_ID2 = o.R_ID
group by o.OR_ID, o.CNT, T3.L_ID
)
select x.OR_ID, x.CNT, x.L_ID, x.CNT_T3
from x
where CNT = CNT_T3
order by x.OR_ID, x.L_ID
OR_ID | CNT | L_ID | CNT_T3 | 1 | 2 | 1001 | 2 | 1 | 2 | 1004 | 2 | 2 | 2 | 1003 | 2 |
---|