我有两张桌子 -
SUBJECT_TABLE
SUBJECT_UID SUBJECT | PARENT_SUBJECT_UID | SUBJECT_LEVEL
SUBJECT_ITEM_TABLE
SUBJECT_ITEM_UID | SUBJECT_UID | ACTIVE_DATE
要求:
感谢任何帮助。感谢。
答案 0 :(得分:0)
如果您只想检查每个父级的子行,则可以使用此查询:
with st as (
select subject_uid suid, parent_subject_uid puid,
nvl((select 1 from subject_item si
where si.subject_uid = st.subject_uid and active_date<sysdate ), 0) it
from subject_table st)
select suid, puid, it from st
where it=1 or exists (select 1 from st st2 where it=1 and puid=st.suid)
但是如果你想对子女,孙子女等进行检查,那么你需要分层子查询:
with st as (
select subject_uid suid, parent_subject_uid puid,
nvl((select 1 from subject_item si
where si.subject_uid = st.subject_uid and active_date<sysdate ), 0) it
from subject_table st),
sh as (
select suid, puid, it, connect_by_root(suid) root from st
connect by prior suid= puid )
select suid, puid, it from st
where it=1 or exists (select 1 from sh where it=1 and root=st.suid)
在我的示例中,id = 2的行没有项目,他的孩子也没有,但是孙子符合标准,所以第二个查询也显示id = 2。