ORA-00936: 使用数据透视表时缺少表达式
ORA-00936: missing expression while using pivot
我需要旋转 table。以下是我的查询。
CREATE TABLE SurveyResponses (
id int,
question varchar(255),
answer varchar(255)
);
INSERT INTO SurveyResponses (id, question, answer) values (1, 'question 1', 'responseX');
INSERT INTO SurveyResponses (id, question, answer) values (2, 'question 1', 'responseY');
INSERT INTO SurveyResponses (id, question, answer) values (3, 'question 1', 'responseZ');
INSERT INTO SurveyResponses (id, question, answer) values (4, 'question 2', 'responseA');
INSERT INTO SurveyResponses (id, question, answer) values (5, 'question 2', 'responseB');
INSERT INTO SurveyResponses (id, question, answer) values (6, 'question 3', 'responseC');
当我尝试使用 pivot the table 并将问题作为列时,我得到
ORA-00936: missing expression
我的查询中缺少什么?
我希望它是动态的,因为我在真实 table.
中有 500 多个问题
查询
SELECT * FROM (
SELECT id, question, answer from SurveyResponses
) SurveyResponsesResults
PIVOT (
MAX(answer)
FOR question
In(
SELECT DISTINCT question FROM SurveyResponses
)
) As pivottable;
IN
不能 动态。使用第 7 行而不是(注释的)第 8 行。
SQL> select * from (
2 select id, question, answer from SurveyResponses
3 ) SurveyResponsesResults
4 PIVOT (
5 max(answer)
6 For question
7 In('question 1' as q1, 'question 2' as q2, 'question 3' as q3
8 --SELECT DISTINCT question FROM SurveyResponses
9 )
10 ) ;
ID Q1 Q2 Q3
---------- ---------- ---------- ----------
1 responseX
6 responseC
2 responseY
4 responseA
5 responseB
3 responseZ
6 rows selected.
SQL>
扩展@Littlefoot 的回答。
如果我必须在 IN
中使用动态输入。
我将不得不使用 PIVOT XML
和 xmlserialize
查询如下。
SELECT xmlserialize(content pivottable.question_XML) from (
SELECT id, question, answer from SurveyResponses
) SurveyResponsesResults
PIVOT XML (
Max(answer)
FOR question
In(
SELECT DISTINCT question FROM SurveyResponses
)
) pivottable ;
查询有效,但它以 XML 格式输出。
我正在尝试弄清楚如何将 XML 转换为 TABLE。如果您知道,请随时编辑。
我需要旋转 table。以下是我的查询。
CREATE TABLE SurveyResponses (
id int,
question varchar(255),
answer varchar(255)
);
INSERT INTO SurveyResponses (id, question, answer) values (1, 'question 1', 'responseX');
INSERT INTO SurveyResponses (id, question, answer) values (2, 'question 1', 'responseY');
INSERT INTO SurveyResponses (id, question, answer) values (3, 'question 1', 'responseZ');
INSERT INTO SurveyResponses (id, question, answer) values (4, 'question 2', 'responseA');
INSERT INTO SurveyResponses (id, question, answer) values (5, 'question 2', 'responseB');
INSERT INTO SurveyResponses (id, question, answer) values (6, 'question 3', 'responseC');
当我尝试使用 pivot the table 并将问题作为列时,我得到
ORA-00936: missing expression
我的查询中缺少什么? 我希望它是动态的,因为我在真实 table.
中有 500 多个问题查询
SELECT * FROM (
SELECT id, question, answer from SurveyResponses
) SurveyResponsesResults
PIVOT (
MAX(answer)
FOR question
In(
SELECT DISTINCT question FROM SurveyResponses
)
) As pivottable;
IN
不能 动态。使用第 7 行而不是(注释的)第 8 行。
SQL> select * from (
2 select id, question, answer from SurveyResponses
3 ) SurveyResponsesResults
4 PIVOT (
5 max(answer)
6 For question
7 In('question 1' as q1, 'question 2' as q2, 'question 3' as q3
8 --SELECT DISTINCT question FROM SurveyResponses
9 )
10 ) ;
ID Q1 Q2 Q3
---------- ---------- ---------- ----------
1 responseX
6 responseC
2 responseY
4 responseA
5 responseB
3 responseZ
6 rows selected.
SQL>
扩展@Littlefoot 的回答。
如果我必须在 IN
中使用动态输入。
我将不得不使用 PIVOT XML
和 xmlserialize
查询如下。
SELECT xmlserialize(content pivottable.question_XML) from (
SELECT id, question, answer from SurveyResponses
) SurveyResponsesResults
PIVOT XML (
Max(answer)
FOR question
In(
SELECT DISTINCT question FROM SurveyResponses
)
) pivottable ;
查询有效,但它以 XML 格式输出。
我正在尝试弄清楚如何将 XML 转换为 TABLE。如果您知道,请随时编辑。