使用unpivot的复杂Oracle Select子句

时间:2016-11-07 22:21:16

标签: oracle11g

请参阅下面的查询:

SELECT QUESTIONID, CORRECT_OPTION, CHOICE
FROM   QUESTION_RAW
    UNPIVOT (Choice FOR QUSTN IN (OPTION1 AS 'A', OPTION2 AS 'B', OPTION3 AS 'C', OPTION4 AS 'D'))
where QUESTIONID = 1

它给出了如下输出:

Questionid CORRECT_OPTION CHOICE
---------------------------------
1         OPTION3         1 Day
1         OPTION3         2 Days
1         OPTION3         3 Days
1         OPTION3         None

注意:我想显示' Y'而不是OPTION3用于第3条记录,而对于休息,我想要显示“N' N'如下所示

Questionid CORRECT_OPTION CHOICE
---------------------------------
1         N          1 Day
1         N          2 Days
1         Y          3 Days
1         N          None

我是否可以在select子句中进行一些计算(如大小写)并显示如上所示的输出:

1 个答案:

答案 0 :(得分:0)

您忽略了for QUSTN IN (OPTION1 as 'A'等,这使得编写正确的CASE表达式变得困难。相反,取消for QUSTN IN (OPTION1 as 'OPTION1', OPTION2 as 'OPTION2'等。

然后,选择QUESTIONID, CORRECT_OPTION, CHOICE

,而不是选择QUESTIONID, CASE WHEN CORRECT_OPTION = QUSTN THEN 'Y' ELSE 'N' END as CORRECT_OPTION, CHOICE ...