I have two tables say Person(person_id, name) and another table Contacts(person_id, phone_type, phone_no).
Person ----------------- person_id name ----------------- P1 Abc P2 Xyz
Contacts -------------------------------- person_id phone_type phone_no -------------------------------- P1 phone1 12345 P1 phone2 23455 P2 phone1 67897 P2 phone3 89786
I need to create a view v_pc which looks something like
v_pc person_id name phone1 phone2 phone3 ------------------------------------- P1 Abc 12345 23455 P2 Xyz 67897 89786
i.e., rows of contacts table are pivot-ed to form columns for the view(number of columns will be variable based on distinct values of 'phone_types' column).
Is there any way I can Pivot the contacts table but use dynamic pivot-in-clause, something like
SELECT *
FROM (
SELECT
person_idd,
phone_type,
phone_no
FROM contacts
) PIVOT (MAX(phone_no) FOR phone_type IN ('phone1','phone2','phone3'))
I also tried using XML clause in with pivot so use dynamic pivot-in-clause ,i.e., extracting result in XML and then recreating columns using XMLTABLE. But I am not able to reach the desired result.