Oracle如何设置数据透视表Sql

时间:2015-04-03 07:01:43

标签: sql oracle

我有一个表格列,例如 Question_Id,Department_Id,Value。

这是我的数据:

enter image description here

enter image description here

我想得到一张这样的表:

这是我的代码:

SELECT *
FROM   (SELECT question_id, department_id,  TO_NUMBER(value)
        FROM   my_pivot_table)
        PIVOT  (SUM( TO_NUMBER(value)) AS sum_quantity FOR (department_id) IN (11 AS a, 12 AS b, 13 AS c, 14 AS d, 5 as e))

ORDER BY question_id;

我收到此错误,但我没有错误

  ORA-01722: invalid number
01722. 00000 -  "invalid number"
*Cause:    
*Action:

编辑:

我发布了我的值列是varchar。所以我改变了我的代码但仍然没有得到我的数据透视表。

1 个答案:

答案 0 :(得分:0)

看起来您需要移动括号:

SELECT *
FROM   (SELECT question_id, department_id,  TO_NUMBER(value)
        FROM   my_pivot_table  -- right parenthesis was here
        PIVOT  (SUM( TO_NUMBER(value)) AS sum_quantity 
                FOR (department_id) IN (11 AS a, 12 AS b, 13 AS c, 
                     14 AS d, 5 as e)))  -- and moved here
ORDER BY question_id;