1
我想在Oralce的11g的一個以下CTE查詢,但我得到一個錯誤說甲骨文CTE查詢:數據類型錯誤ORA-00932
ORA-00932:不一致的數據類型:預期數量得到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
你想'level1'等這些初始值是零?它看起來像查詢工程,如果你改變它爲0看到這個演示 - http://sqlfiddle.com/#!4/dac35/1 – Taryn
謝謝,這解決了這個問題,你可以做出這個答案,所以我可以接受? – jrara
你也可以使用「cast(null作爲數字)作爲level2」等 –