2012-12-04 51 views
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 
+1

你想'level1'等這些初始值是零?它看起來像查詢工程,如果你改變它爲0看到這個演示 - http://sqlfiddle.com/#!4/dac35/1 – Taryn

+0

謝謝,這解決了這個問題,你可以做出這個答案,所以我可以接受? – jrara

+1

你也可以使用「cast(null作爲數字)作爲level2」等 –

回答

4

如果您更改查詢的第一部分使用零作爲值level1等代替null查詢將工作:

with Tree (EMP_ID, MGR_ID, lv, level1, level2, level3, level4, level5) as 
     (
     SELECT EMP_ID 
     ,  MGR_ID 
     ,  1 as lv 
     ,  1 as level1 
     ,  0 as level2 
     ,  0 as level3 
     ,  0 as level4 
     ,  0 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 

SQL Fiddle with Demo

結果:

| EMP_ID | MGR_ID | LV | LEVEL1 | LEVEL2 | LEVEL3 | LEVEL4 | LEVEL5 | 
--------------------------------------------------------------------- 
|  1 | (null) | 1 |  1 |  0 |  0 |  0 |  0 | 
|  2 |  1 | 2 |  1 |  2 |  0 |  0 |  0 | 
|  3 |  1 | 2 |  1 |  3 |  0 |  0 |  0 | 
|  4 |  2 | 3 |  1 |  2 |  4 |  0 |  0 | 
|  5 |  2 | 3 |  1 |  2 |  5 |  0 |  0 | 
|  6 |  2 | 3 |  1 |  2 |  6 |  0 |  0 | 
|  7 |  3 | 3 |  1 |  3 |  7 |  0 |  0 | 
|  8 |  5 | 4 |  1 |  2 |  5 |  8 |  0 | 
|  9 |  7 | 4 |  1 |  3 |  7 |  9 |  0 | 
|  10 |  5 | 4 |  1 |  2 |  5 |  10 |  0 | 
|  11 |  7 | 4 |  1 |  3 |  7 |  11 |  0 | 
|  12 |  9 | 5 |  1 |  3 |  7 |  9 |  12 | 
|  13 |  9 | 5 |  1 |  3 |  7 |  9 |  13 | 
|  14 |  9 | 5 |  1 |  3 |  7 |  9 |  14 | 

或者您也可以cast(null as number)和查詢將工作:

with Tree (EMP_ID, MGR_ID, lv, level1, level2, level3, level4, level5) as 
     (
     SELECT EMP_ID 
     ,  MGR_ID 
     ,  1 as lv 
     ,  1 as level1 
     ,  cast(null as number) as level2 
     ,  cast(null as number) as level3 
     ,  cast(null as number) as level4 
     ,  cast(null as number) 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 

SQL Fiddle with Demo