我正在 Oralce 11g 中尝试以下 CTE 查询,但我收到一条错误消息
ORA-00932: 不一致的数据类型: 预期 NUMBER 得到 CHAR。
任何想法这是什么根本原因?
create table PC (
EMP_ID NUMBER NULL,
MGR_ID NUMBER NULL
);
INSERT INTO PC (EMP_ID, MGR_ID)
VALUES (1.0, NULL);
INSERT INTO PC (EMP_ID, MGR_ID)
VALUES (2.0, 1.0);
INSERT INTO PC (EMP_ID, MGR_ID)
VALUES (3.0, 1.0);
INSERT INTO PC (EMP_ID, MGR_ID)
VALUES (4.0, 2.0);
INSERT INTO PC (EMP_ID, MGR_ID)
VALUES (5.0, 2.0);
INSERT INTO PC (EMP_ID, MGR_ID)
VALUES (6.0, 2.0);
INSERT INTO PC (EMP_ID, MGR_ID)
VALUES (7.0, 3.0);
INSERT INTO PC (EMP_ID, MGR_ID)
VALUES (8.0, 5.0);
INSERT INTO PC (EMP_ID, MGR_ID)
VALUES (9.0, 7.0);
INSERT INTO PC (EMP_ID, MGR_ID)
VALUES (10.0, 5.0);
INSERT INTO PC (EMP_ID, MGR_ID)
VALUES (11.0, 7.0);
INSERT INTO PC (EMP_ID, MGR_ID)
VALUES (12.0, 9.0);
INSERT INTO PC (EMP_ID, MGR_ID)
VALUES (13.0, 9.0);
INSERT INTO PC (EMP_ID, MGR_ID)
VALUES (14.0, 9.0);
SELECT * FROM pc;
with Tree (EMP_ID, MGR_ID, lv, level1, level2, level3, level4, level5) as
(
SELECT EMP_ID
, MGR_ID
, 1 as lv
, 1 as level1
, null as level2
, null as level3
, null as level4
, null as level5
FROM PC
WHERE MGR_ID IS NULL
UNION ALL
SELECT E.EMP_ID
, E.MGR_ID
, T.lv + 1
, case when T.lv + 1 = 1 then E.EMP_ID else T.level1 end
, case when T.lv + 1 = 2 then E.EMP_ID else T.level2 end
, case when T.lv + 1 = 3 then E.EMP_ID else T.level3 end
, case when T.lv + 1 = 4 then E.EMP_ID else T.level4 end
, case when T.lv + 1 = 5 then E.EMP_ID else T.level5 end
FROM Tree T inner join PC E
ON (T.EMP_ID = E.MGR_ID)
)
select *
from Tree