Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle SQL Developer: How to transpose rows to columns using PIVOT function

I'm attempting to create a query to transpose rows into columns using the PIVOT function.

This is the contact table I want to transpose into rows:

   PARTYID CONTACTTEXT  CONTACTTYPECD
---------- ------------ -------------
       100 0354441010               1
       100 0355551010               2
       100 0428105789               3
       100 [email protected]             4

My intended result:

   PARTYID PHONE        FAX          MOBILE       EMAIL      
---------- ------------ ------------ ------------ ------------
       100 0354441010   0355551010   0428105789   [email protected]

My query:

SELECT * FROM 
  ( 
    SELECT partyId, contacttext, contacttypecd 
    FROM CONTACT 
    WHERE partyId = 100; 
  ) 
  PIVOT ( 
    MAX(contacttext) 
  FOR contacttypecd in (1 Phone, 2 Fax, 3 Mobile, 4 Email)); 

Errors I'm getting:

Error starting at line 9 in command: 
FOR contacttypecd in (1 Phone, 2 Fax, 3 Mobile, 4 Email)) 
Error report: 
Unknown Command 

The reason for my problem was because my Oracle database version (Oracle9i) did not support the PIVOT function. Here's how to do it in a 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
FROM 
  (
    SELECT partyid, contacttext, contacttypecd
    FROM CONTACT
    WHERE partyid = 100
  ) t
 GROUP BY PartyID
like image 524
user1309226 Avatar asked Apr 29 '15 13:04

user1309226


People also ask

How do I convert a row to a column in SQL Developer?

Syntax / Sample SELECT * FROM ( SELECT column1,column2 FROM tables WHERE conditions ) PIVOT ( aggregate_function(column2) FOR column2 IN ( expr1, expr2, ... expr_n ) | subquery ) order by expression[asc | desc];

How do I PIVOT data in SQL Developer?

Example. SELECT * FROM (SELECT SUBSTR(time_id,06,02) AS month_val, prod_id, amount_sold FROM sales WHERE SUBSTR(time_id,01,04) = 1998 ) PIVOT ( SUM(amount_sold) FOR (month_val, prod_id) IN ((01,12008), (02, 12010) ,(03, 12011 ) )); Further more, we can also have multiple aggregate functions in our PIVOT query.

How convert rows to columns decode in Oracle?

emp ) SELECT job , MIN (DECODE (r_num, 1, ename)) AS name1 , MIN (DECODE (r_num, 2, ename)) AS name2 , MIN (DECODE (r_num, 3, ename)) AS name3 FROM got_r_num GROUP BY job ORDER BY job ; This assumes that we know an upper bound to the numebr of pivoted columns (3 in this example).


1 Answers

You have a stray semi-colon in your statement, after:

    WHERE partyId = 100; 

Remove that to make it:

SELECT * FROM 
  ( 
    SELECT partyId, contacttext, contacttypecd 
    FROM CONTACT 
    WHERE partyId = 100
  ) 
  PIVOT ( 
    MAX(contacttext) 
  FOR contacttypecd in (1 Phone, 2 Fax, 3 Mobile, 4 Email));

   PARTYID PHONE        FAX          MOBILE       EMAIL      
---------- ------------ ------------ ------------ ------------
       100 0354441010   0355551010   0428105789   [email protected]

It's being seen as multiple statements; the first is incomplete because it's missing a closing parenthesis (so gets ORA-00907), the second starts with that parenthesis and gets the error you reported, and then each subsequent line gets the same error. You only seem to be looking at the last reported error - it's usually much more helpful to start with the first error, clear that, and then move onto the next if it still exists.

like image 139
Alex Poole Avatar answered Sep 20 '22 08:09

Alex Poole