如何在Oracle中透视此数据?

时间:2015-10-16 20:16:13

标签: sql oracle pivot

我有一个查询会吐出以下数据:

 ID      | RESPONSE_VALUE                       | TITLE
---------------------------------------------------------------------
 1634303 | 6th Grade - Referring teacher's room | Academic_Area
 1634303 | doing stuff and thangs               | Help
 1634303 | Doe, Jane                            | Name
 1634303 | Doe, John                            | Name_of_Student
 1634303 | 1 Day                                | Days
 1634303 | Tuesday                              | Preferred_PAWs_Day
 1634303 | Online                               | materials

我想转移这些数据,看起来像这样:

   ID    | Academic_Area                          | Help                     | Name        | Name_of_Student | Days    | Preferred_PAWs_Day | materials
--------------------------------------------------------------------------------------------------------------------------------------------------------
 1634303 | "6th Grade - Referring teacher's room" | "doing stuff and thangs" | "Doe, Jane" | "John,  Doe"    | "1 Day" | Tuesday            | ""

由于这种数据实际上没有一个有意义的聚合函数,我没有看到一种很好的方法来转动数据。

1 个答案:

答案 0 :(得分:2)

您可以使用listagg()max()(如果只涉及一个值)。

例如:

select id,
       max(case when response_value = '6th Grade - Referring teacher''s room'
                then title
           end) as "6th Grade - Referring teacher's room",
       . . .
from t
group by id;