Oracle SQL Developer: How to transpose rows to columns using PIVOT function -
i'm attempting create query transpose rows columns using pivot function.
this contact
table want transpose rows:
partyid contacttext contacttypecd ---------- ------------ ------------- 100 0354441010 1 100 0355551010 2 100 0428105789 3 100 abc@home.com 4
my intended result:
partyid phone fax mobile email ---------- ------------ ------------ ------------ ------------ 100 0354441010 0355551010 0428105789 abc@home.com
my query:
select * ( select partyid, contacttext, contacttypecd contact partyid = 100; ) pivot ( max(contacttext) contacttypecd in (1 phone, 2 fax, 3 mobile, 4 email));
errors i'm getting:
error starting @ line 9 in command: contacttypecd in (1 phone, 2 fax, 3 mobile, 4 email)) error report: unknown command
the reason problem because oracle database version (oracle9i) did not support pivot function. here's how in different way:
select partycd ,max(decode(t.contacttypecd, 1, t.contacttext)) phone ,max(decode(t.contacttypecd, 2, t.contacttext)) fax ,max(decode(t.contacttypecd, 3, t.contacttext)) mobile ,max(decode(t.contacttypecd, 4, t.contacttext)) email ( select partyid, contacttext, contacttypecd contact partyid = 100 ) t group partyid
you have stray semi-colon in statement, after:
partyid = 100;
remove make it:
select * ( select partyid, contacttext, contacttypecd contact partyid = 100 ) pivot ( max(contacttext) contacttypecd in (1 phone, 2 fax, 3 mobile, 4 email)); partyid phone fax mobile email ---------- ------------ ------------ ------------ ------------ 100 0354441010 0355551010 0428105789 abc@home.com
it's being seen multiple statements; first incomplete because it's missing closing parenthesis (so gets ora-00907), second starts parenthesis , gets error reported, , each subsequent line gets same error. seem looking @ last reported error - it's more helpful start first error, clear that, , move onto next if still exists.
Comments
Post a Comment