【发布时间】:2021-06-17 02:39:24
【问题描述】:
我需要旋转表格。以下是我的疑问。
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');
当我尝试使用带有问题的表格作为列时,我得到了
ORA-00936:缺少表达式
我的查询中缺少什么? 我希望它是动态的,因为我在真实表格中有 500 多个问题。
查询
SELECT * FROM (
SELECT id, question, answer from SurveyResponses
) SurveyResponsesResults
PIVOT (
MAX(answer)
FOR question
In(
SELECT DISTINCT question FROM SurveyResponses
)
) As pivottable;
【问题讨论】:
标签: oracle pivot-table