Home » SQL & PL/SQL » SQL & PL/SQL » Matrix/Column transformation
Matrix/Column transformation [message #23371] Wed, 04 December 2002 08:39 Go to next message
Mike
Messages: 417
Registered: September 1998
Senior Member
Hi OracleSuperman,

can you help me with my problem ? I need to know how to transformate values from rows into columns. I did it in MSSQL before 5 years (I forgot how:) ). Does exist some solutions in Oracle ?

Help me please

Mike

Problem example description :

Create table Exchange

(
Currency varchar2(3),
Rate number(20,5)
)

Insert into Exchange (Currency,Rate) values ('USD',1);
Insert into Exchange (Currency,Rate) values ('EUR',2);
Insert into Exchange (Currency,Rate) values ('GBP',3);

Input matrix : select currency,rate from exchange

CURRENCY | RATE
=======================
USD | 1
EUR | 2
GBP | 3

Output matrix : ??? Unknow SQL ???????

USD | EUR | GBP
==================
1 | 2 | 3
Re: Matrix/Column transformation [message #23379 is a reply to message #23371] Wed, 04 December 2002 12:04 Go to previous messageGo to next message
andrew again
Messages: 2577
Registered: March 2000
Senior Member
SELECT max(DECODE (currency, 'USD', rate)) USD,
       max(DECODE (currency, 'EUR', rate)) EUR,
       max(DECODE (currency, 'GBP', rate)) GBP
  FROM exchange;

USD	EUR	GBP
1	2	3

select max(case 
                 when currency = 'USD' then rate
                 else null
            end) USD,
		max(case  
                 when currency ='EUR' then rate
                 else null
            end) EUR,
		max(case 
                 when currency ='GBP' then rate
                 else null
            end) GBP
from exchange;

USD	EUR	GBP
1	2	3

SELECT   object_type, 
	 SUM (DECODE (owner, 'SCOTT', 1, 0)) scott_count,
	 SUM (DECODE (owner, 'TOAD', 1, 0)) toad_count
    FROM all_objects
   WHERE owner IN ('SCOTT', 'TOAD')
GROUP BY object_type;

OBJECT_TYPE	SCOTT_COUNT  TOAD_COUNT
--------------- -----------  ----------
INDEX		2		7
PACKAGE		0		1
PACKAGE BODY	0		1
SEQUENCE	0		1
TABLE		5		7
TRIGGER		1		0
VIEW		0		2

Re: Matrix/Column transformation [message #23393 is a reply to message #23371] Thu, 05 December 2002 01:14 Go to previous messageGo to next message
Mike
Messages: 417
Registered: September 1998
Senior Member
Thank you Andrew,

I am sorry, that I did not explain my problem exactly. I know these solutions. I am finding solutions, when I don't know names of columns (USD,GBP,USD etc.). I need some generally solution. I need some dynamic transformation if I dosn't know columns names before transformation.
I have temporary solutions. I create SQL View by PLSQL (I found column names and create "static view" as soluted above this text - in your replay).
Do you know some another way to solve it ?

Thank you again

Mike
Re: Matrix/Column transformation [message #23403 is a reply to message #23371] Thu, 05 December 2002 11:33 Go to previous messageGo to next message
andrew again
Messages: 2577
Registered: March 2000
Senior Member
See the URL below. You can populate the array by selecting distinct currency from your table.

Re: Matrix/Column transformation [message #23422 is a reply to message #23371] Fri, 06 December 2002 07:01 Go to previous message
Mike
Messages: 417
Registered: September 1998
Senior Member
Thank you
Previous Topic: Parse a string
Next Topic: HELP
Goto Forum:
  


Current Time: Thu May 16 02:25:53 CDT 2024