I have a table that looks like this:
C_ID P_ID KEY VALUE
null null KEY1 VALUE1
null null KEY2 VALUE2
null null KEY3 VALUE3
2 2 KEY4 VALUE4
2 3 KEY5 VALUE5
I want to get this result table/view:
C_ID P_ID KEY1 KEY2 KEY3 KEY4 KEY5
NULL NULL VALUE1 VALUE2 VALUE3 NULL NULL
2 2 NULL NULL NULL VALUE4 NULL
2 3 NULL NULL NULL NULL VALUE5
Has anybody an Idea how I could achieve this?
I have tried it with:
select * from (select c_id, p_id, r_key, r_value from s_projectroles) pivot (max(r_value) for r_key in (any));
I got an error:
ORA-00936: Ausdruck fehlt
00936. 00000 - "missing expression"
This can be done dynamically the following way. First, here is the static version of the query so you can see the final sql:
See SQL Fiddle with Demo
Then to do this dynamically, you can create the following procedure:
Then to execute it:
The result is the same: