基于多列的Oracle Pivot

时间:2018-08-09 19:09:28

标签: sql oracle pivot oracle-sqldeveloper

我对Pivoting还是很陌生,并尝试根据两列进行Pivot。

我拥有的数据:

enter image description here

数据透视后要获取的数据:

enter image description here

我的查询有缺陷:

   select *
from
(
  select ISSUEID,ANSWER, ANSWERCOMMENT,QUESTION ,QUESTIONID
  from issue_survey
  WHERE ISSUEID = 6877
) d
pivot
(
  max(QUESTION)
  for QUESTIONID  in (1 QUESTION1,2 QUESTION2, 3 QUESTION3)
) piv;

此错误查询所获得的结果:

enter image description here

任何与此有关的建议,我们都会感激。谢谢!

1 个答案:

答案 0 :(得分:1)

尝试:

<!DOCTYPE html>

<head>
  <style>
    ul li {
      margin: "5em 5em 5em 5em";
      border: "5em 5em 5em 5em";
      padding: "5em 5em 5em 5em";
    }
  </style>
</head>

<body>
  <ul>
    <li> foo </li>
    <ul>
      <li> a </li>
      <li> b </li>
      <li> c </li>
    </ul>
    <li> bar </li>
  </ul>
</body>

</html>

演示:http://www.sqlfiddle.com/#!4/e5aba7/6

select *
from
(
  select ISSUEID,ANSWER, ANSWERCOMMENT,QUESTION ,
         rownum rn
  from issue_survey
  WHERE ISSUEID = 6877
) d
pivot
(
  max(QUESTION) as question, max(Answer) as answer,
  max( ANSWERCOMMENT ) as ANSWERCOMMENT
  for rn  in ( 1 ,2 , 3 )
) piv;