Since you are on Oracle 10g, there is no PIVOT
function. So you can replicate it using a CASE
statement with an aggregate function. If you know the values that you want as columns, then you can hard-code it:
select p_id,
max(case when p_name = 'chetan' then p_value end) chetan,
max(case when p_name = 'john' then p_value end) john,
max(case when p_name = 'paul' then p_value end) paul
from table1
group by p_id
See SQL Fiddle with Demo
If you do not know the values, then you will want to use a dynamic version of this, similar to this:
CREATE OR REPLACE procedure dynamic_pivot(p_cursor in out sys_refcursor)
as
sql_query varchar2(1000) := 'select P_id ';
begin
for x in (select distinct P_name from table1 order by 1)
loop
sql_query := sql_query ||
' , max(case when P_name = '''||x.P_name||''' then P_value end) as '||x.P_name;
dbms_output.put_line(sql_query);
end loop;
sql_query := sql_query || ' from Table1 group by P_id';
open p_cursor for sql_query;
end;
/
Then to execute it, you can use:
variable x refcursor
exec dynamic_pivot(:x)
print x
The results would be:
P_ID CHETAN JOHN PAUL
---------- ---------- ---------- ----------
123 34 83 38
273 54 24 34