我有一个 3 层的 apex 树:(Apex 版本 22.1)
BU_NAME ORG_UNIT_NAME 部门名称
Source Table:
DEPT_BU_LIST
DEPT_ID BU_NAME ORG_UNIT_NAME DEPT_NAME
1 A_BU AX_ORG A1_DEPT
2 B_BU BX_ORG A2_DEPT
3 C_BU CX_ORG A3_DEPT
4 C_BU CX_ORG A4_DEPT
CREATE TABLE DEPT_BU_LIST
( DEPT_ID NOT NULL ENABLE,
BU_NAME VARCHAR2(100), ORG_UNIT_NAME VARCHAR2(100), DEPT_NAME VARCHAR2(100));
INSERT INTO DEPT_BU_LIST VALUES
(1,'A_BU','AX_ORG','A1_DEPT');
INSERT INTO DEPT_BU_LIST VALUES
(1,'B_BU','AY_ORG','A1_DEPT');
INSERT INTO DEPT_BU_LIST VALUES
(1,'C_BU','CX_ORG','A3_DEPT');
INSERT INTO DEPT_BU_LIST VALUES
(1,'C_BU','CX_ORG','A4_DEPT');
当我在 apex 中执行以下查询时,前 2 个级别正确显示数据,但 dept_name 值重复。
例如:
A_BU
AX_ORG
A1_DEPT
A2_DEPT
B_BU
AY_ORG
A1_DEPT
A2_DEPT
即使在直通表中A_BU有A_DEPT并且B_BU有B1_DEPT分配。 它显示的数据如上。
SELECT case WHEN connect_by_isleaf = 1 THEN 0
WHEN level = 1 THEN 1
ELSE -1
END status,
level "level",
display name,
null as icon,
id id,
tooltip tooltip,
link link
FROM (
SELECT bu_name
display,
, null pid
, bu_name id
, 1 "level"
, 'javascript:$s(''P1_SELECTED_NODE'',
'''||bu_name||'||||'||''')'
link
, null tooltip
FROM (SELECT bu_name bu_name
, org_unit_name org_unit_name
, dept_name dept_name
, count(1) error_count
FROM dept_bu_list
GROUP BY
bu_name
, org_unit_name
,dept_name
) e
GROUP BY
bu_name
UNION ALL
SELECT org_unit_name
display,
, bu_name pid
, org_unit_name id
, 2 "level"
, 'javascript:$s(''P1_SELECTED_NODE'', '''||bu_name||'|'||org_unit_name||'|||'||''')'
link
, null tooltip
FROM (SELECT bu_name bu_name
, org_unit_name org_unit_name
, dept_name dept_name
, count(1) error_count
FROM dept_bu_list
GROUP BY
bu_name
, org_unit_name
,dept_name
) e
GROUP BY
bu_name
, org_unit_name
UNION ALL
SELECT dept_name
display,
, org_unit_name pid
, dept_name id
, 3 "level"
, 'javascript:$s(''P1_SELECTED_NODE'', '''||bu_name||'|'||org_unit_name||'|'||dept_name||'||'||''')'
link
, null tooltip
FROM (SELECT bu_name bu_name
, org_unit_name org_unit_name
, dept_name dept_name
, count(1) error_count
FROM dept_bu_list
GROUP BY
bu_name
, org_unit_name
,dept_name
) e
GROUP BY
bu_name
, org_unit_name
, dept_name
)
START WITH pid IS NULL
CONNECT BY PRIOR id = pid
ORDER SIBLINGS BY display
请帮忙解决这个问题..我想我在第 3 级缺少一些东西,它返回所有 dept_name 值
不清楚您期望示例数据的输出是什么,但您似乎想要显示层次结构。为此,您可以
UNPIVOT
您的数据并删除重复项,然后使用分层查询将其重建为树结构:
SELECT child, depth
FROM (
SELECT DISTINCT parent, child, depth
FROM (SELECT d.*, NULL AS root FROM DEPT_BU_LIST d)
UNPIVOT (
(parent, child) FOR depth IN (
(root, bu_name) AS 1,
(bu_name, org_unit_name) AS 2,
(org_unit_name, dept_name) AS 3
)
)
)
START WITH DEPTH = 1
CONNECT BY PRIOR child = parent
AND PRIOR depth + 1 = depth
ORDER SIBLINGS BY child
对于样本数据:
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';
CREATE TABLE DEPT_BU_LIST (
DEPT_ID NUMBER PRIMARY KEY NOT NULL ENABLE,
BU_NAME VARCHAR2(100),
ORG_UNIT_NAME VARCHAR2(100),
DEPT_NAME VARCHAR2(100)
);
BEGIN
INSERT INTO DEPT_BU_LIST VALUES (1,'A_BU','AX_ORG','A1_DEPT');
INSERT INTO DEPT_BU_LIST VALUES (2,'B_BU','AY_ORG','A1_DEPT');
INSERT INTO DEPT_BU_LIST VALUES (3,'C_BU','CX_ORG','A3_DEPT');
INSERT INTO DEPT_BU_LIST VALUES (4,'C_BU','CX_ORG','A4_DEPT');
END;
/
输出:
孩子 | 深度 |
---|---|
A_BU | 1 |
AX_ORG | 2 |
A1_部门 | 3 |
B_BU | 1 |
AY_ORG | 2 |
A1_部门 | 3 |
C_BU | 1 |
CX_ORG | 2 |
A3_部门 | 3 |
A4_部门 | 3 |