I need help with something similar to PIVOT, but it isn't that easy.
I have table with this structure, and i need to get structure something like in the bottom of the picture:

Can anyone help me? Thank you guys!
Here is a solution using a pivot:
with testdata as
(select 'first' casekey, 1 custom_field_id, 'value1' custom_field_value from dual union all
select 'first' casekey, 2 custom_field_id, 'value2' custom_field_value from dual union all
select 'first' casekey, 3 custom_field_id, 'value3' custom_field_value from dual union all
select 'second' casekey, 6 custom_field_id, 'value1' custom_field_value from dual union all
select 'second' casekey, 9 custom_field_id, 'value1' custom_field_value from dual)
select *
from(
select casekey, custom_field_id, custom_field_value,
rank() over(partition by casekey order by custom_field_id) pivotRank
from testdata)
pivot(
max(custom_field_id || ':' || custom_field_value)
for pivotRank in (1 as CF1, 2 as CF2, 3 as CF3, 4 as CF4, 5 as CF5)
)
First I use a windowing function to rank the custom_field_id column partitioned by casekey. Then all you have to do is take the max of the concatenated fields you wanted and pivot 1 through 5 on the pivotRank.
My output for the above query looks like this:
casekey CF1 CF2 CF3 CF4 CF5
first 1:value1 2:value2 3:value3 (null) (null)
second 6:value1 9:value1 (null) (null) (null)
try this:
with y as (
select cas, cfi || ':' || cfv cf, ROW_NUMBER() over (partition by cas order by cfi) n
from x)
, z AS (
select DISTINCT cas
FROM x
)
select z.cas, cf1.cf, cf2.cf, cf3.cf, cf4.cf, cf5.cf
from z
left join y as cf1 on z.cas = cf1.cas and cf1.n = 1
left join y as cf2 on z.cas = cf2.cas and cf1.n = 2
left join y as cf3 on z.cas = cf3.cas and cf1.n = 3
left join y as cf4 on z.cas = cf4.cas and cf1.n = 4
left join y as cf5 on z.cas = cf5.cas and cf1.n = 5
case statementfor each column