I have the following table
P_ID, PROGR, DATA
1 , 1 , 'DATO A'
1 , 2 , 'DATO B'
1 , 3 , 'DATO C'
2 , 1 , 'DATO D'
2 , 2 , 'DATO E'
3 , 1 , 'DATO G'
and I want to get this result
P_ID, DATA , DATA_1 , DATA_2
1 , 'DATO A', 'DATO B', 'DATO C'
2 , 'DATO D', 'DATO E', NULL
3 , 'DATO G', NULL , NULL
this can be done with a left join with the same table, something like this (not the exact result, but as an example)
select * from
(select * from MYTABLE where PROGR = 1) a
left join
(select * from MYTABLE where PROGR = 2) b
on a.P_ID = b.P_ID
left join
(select * from MYTABLE where PROGR = 3) c
on a.P_ID = c.P_ID;
The problem is that this query is fixed, and need to be rewritten if some P_ID get PROGR = 4. I think that I need to make a procedure, but I have been trying without success.
Thanks in advance.