Hi all, consider the following data
WITH data1 as
(
select 123 pid, 555 ad_id, 'pox' imm from dual union all
select 222 pid, 666 ad_id, 'pox' imm from dual union all
select 222 pid, 777 ad_id, 'pox' imm from dual union all
select 333 pid, 888 ad_id, 'pox' imm from dual union all
select 333 pid, 999 ad_id, 'pox' imm from dual
)
I am looking into pivoting the data. each pid will have one or two rows in the table. I want to pivot the data is such a way that produce the following output:
pid imm dose1 dose2 ad_id1 ad_id2
=========================================================
123 pox 1 0 555
222 pox 1 1 666 777
333 pox 1 1 888 999
as mentioned above, each pid can have one or two rows in the table with different ad_id value. each row represent a dosage
i want to pivot the data and create additional columns. for example. pid=123 only has one row so this is one dosage and dose1 column will be 1 and dose2 will be 0 because there is no second row. for pid=222 , we have two rows so dose1 will be 1 and dose2 will be 1 too. also ad_id has two values and therefore we display ad_id1 as 666 abd ad_id2 as 777. same scenario happens for pid=333.
i am thinking pivot function can do the job which i am not too familiar.
i am using oracle 11g. can someone help me write a query to produce the above output.
thanks in advance