我有一个包含用户ID的表和另一个包含层次结构元素的表。具有用户标识的表具有包含层次结构元素的表的唯一键。我想要完成的是显示每个元素 用户的层次表。问题是我有遍历和显示的层次结构节点。以下是我使用的表格以及我使用的数据的示例。
create table fsecure(
userid varchar(100),
f_element varchar(6)
);
REM INSERTING into FSECURE
SET DEFINE OFF;
Insert into FSECURE (USERID,F_ELEMENT) values ('FFA','140');
Insert into FSECURE (USERID,F_ELEMENT) values ('FFA','280');
Insert into FSECURE (USERID,F_ELEMENT) values ('GGA','202118');
Insert into FSECURE (USERID,F_ELEMENT) values ('GGA','140');
Insert into FSECURE (USERID,F_ELEMENT) values ('RFA','202116');
CREATE TABLE "FVAL"
( "FUND" VARCHAR2(6 ),
"FUND_PRED" VARCHAR2(6)
)
REM INSERTING into FVAL
SET DEFINE OFF;
Insert into FVAL (FUND,FUND_PRED) values ('140',null);
Insert into FVAL (FUND,FUND_PRED) values ('280',null);
Insert into FVAL (FUND,FUND_PRED) values ('2000','140');
Insert into FVAL (FUND,FUND_PRED) values ('20001','2000');
Insert into FVAL (FUND,FUND_PRED) values ('20002','2000');
Insert into FVAL (FUND,FUND_PRED) values ('20003','2000');
Insert into FVAL (FUND,FUND_PRED) values ('202118','2000');
Insert into FVAL (FUND,FUND_PRED) values ('2800','280');
Insert into FVAL (FUND,FUND_PRED) values ('280001','2800');
Insert into FVAL (FUND,FUND_PRED) values ('280002','2800');
Insert into FVAL (FUND,FUND_PRED) values ('280003','2800');
Insert into FVAL (FUND,FUND_PRED) values ('280004','2800');
这是一个查询,适用于我只为一个用户执行此操作的时间。
SELECT F.FUND
FROM FVAL F
CONNECT BY PRIOR F.FUND = F.FUND_PRED
START WITH F.FUND IN ( SELECT F_ELEMENT FROM FSECURE WHERE USERID = 'FFA' );
我想使这个通用足以显示每个来自fsecure的用户的元素。
预期输出(注意:我只显示一个用户,但我想为桌面上的每个用户执行此操作)
"FUND" "'FFA'"
"140" "FFA"
"2000" "FFA"
"20001" "FFA"
"20002" "FFA"
"20003" "FFA"
"202118" "FFA"
"280" "FFA"
"2800" "FFA"
"280001" "FFA"
"280002" "FFA"
"280003" "FFA"
"280004" "FFA"
非常感谢任何帮助。
答案 0 :(得分:2)
嗯,据我所知你可以使用类似的东西:
SELECT s.USERID, hier_f.FUND
FROM FSECURE s
JOIN ( SELECT F.FUND, connect_by_root(F.FUND) as root_fund
FROM FVAL F
CONNECT BY PRIOR F.FUND = F.FUND_PRED
START WITH F.FUND IN ( SELECT F_ELEMENT FROM FSECURE)) hier_f
on s.F_ELEMENT = hier_f.root_fund
答案 1 :(得分:2)
我认为这就是您所追求的目标:
SELECT fv.fund,
connect_by_root(fs.userid) userid
FROM fval fv
FULL OUTER JOIN fsecure fs ON (fs.f_element = fv.fund) -- maybe left outer join?
CONNECT BY PRIOR fv.fund = fv.fund_pred
START WITH fs.userid IS NOT NULL
ORDER BY connect_by_root(fs.userid),
connect_by_root(fs.f_element),
LEVEL,
fv.fund;
FUND USERID
------ ------
140 FFA
2000 FFA
20001 FFA
20002 FFA
20003 FFA
202118 FFA
280 FFA
2800 FFA
280001 FFA
280002 FFA
280003 FFA
280004 FFA
140 GGA
2000 GGA
20001 GGA
20002 GGA
20003 GGA
202118 GGA
202118 GGA
RFA
N.B。我不确定您是否希望RFA用户ID显示在输出中,因为它在fval表中没有相应的基金。如果您不想在输出中使用RFA用户标识,则只需将完整的外连接转换为左外连接。