Solved

Row to Column Conversion for Tabletype data

Posted on 2014-03-11
8
212 Views
Last Modified: 2014-12-06
Hi All,

I have two tabletype arrays like below.

tabtype_config_option_ft.extend;
tabtype_config_option_ft(1) := 'COLOR';
tabtype_config_option_ft.extend;
tabtype_config_option_ft(2) := 'WIDTH';
tabtype_config_option_ft.extend;
tabtype_config_option_ft(3) := 'LENGTH';
tabtype_config_option_ft.extend;
tabtype_config_option_ft(4) := 'SIZE';            


tabtype_config_options.extend;
tabtype_config_options(1) := 'RED';
tabtype_config_options.extend;
tabtype_config_options(2) := '1.23';
tabtype_config_options.extend;
tabtype_config_options(3) := '4.56';
tabtype_config_options.extend;
tabtype_config_options(4) := '13';

Open in new window


My requirement is to convert this data from row to column. The expected result should be:

COLOR	WIDTH	LENGTH	SIZE
RED	1.23	4.56	13

Open in new window


Is it possible to perform the row to column conversion?

Thanks
0
Comment
  • 3
  • 2
8 Comments
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 39920793
This is not even SQL!
It's simple array manipulation, what have you tried?
0
 

Author Comment

by:Suriyaraj_Sudalaiappan
ID: 39921341
Hi Mike,

My requirement is, I have one stored procedure with two input parameters like tabtype_config_option_ft and tabtype_config_options. I have to compare this user passed values with some of the SQL queries which is used in the procedure. Since the input data is coming as row type data I am planning to convert that into column and use it in the SQL queries and other calculations.

Usually we can do this row to column conversion if the data is stored in the tables. But here the data is stored in array's.

Thanks
0
 

Author Comment

by:Suriyaraj_Sudalaiappan
ID: 39923009
Assume that I have table data like below

OPTION_FT        OPTION_VAL
COLOR	          RED
WIDTH	          1.23
LENGTH	          4.56
SIZE	                  13

Open in new window


Now i want to convert from to column like below.

COLOR	WIDTH	LENGTH	SIZE
RED	1.23	4.56	13

Open in new window

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 39924010
. . . But here the data is stored in array's.
And what is so difficult about transposing an array?
Arrays are arrays, even in PL/SQL.
0
 
LVL 10

Accepted Solution

by:
dbmullen earned 250 total points
ID: 39932686
you want to PIVOT:

select *
from my_table
pivot (max(option_val) for (option_ft) in (  
'COLOR' as col_color,
'WIDTH' as col_width,  
'LENGTH' as col_length,
'SIZE'   as col_size))
0
 
LVL 29

Assisted Solution

by:MikeOM_DBA
MikeOM_DBA earned 250 total points
ID: 39934440
One way:
SQL> CREATE OR REPLACE TYPE Tabtype IS VARRAY (100) OF VARCHAR2 (40);
  2  /

Type created.

SQL> SET SERVEROUT ON SIZE 1000000;
SQL> DECLARE
  2     Tabtype_Config_Option_Ft Tabtype := Tabtype ();
  3     Tabtype_Config_Options Tabtype := Tabtype ();
  4     I              PLS_INTEGER;
  5     J              PLS_INTEGER;
  6  BEGIN
  7     Tabtype_Config_Option_Ft.EXTEND;
  8     Tabtype_Config_Option_Ft ( 1) := 'COLOR';
  9     Tabtype_Config_Option_Ft.EXTEND;
 10     Tabtype_Config_Option_Ft ( 2) := 'WIDTH';
 11     Tabtype_Config_Option_Ft.EXTEND;
 12     Tabtype_Config_Option_Ft ( 3) := 'LENGTH';
 13     Tabtype_Config_Option_Ft.EXTEND;
 14     Tabtype_Config_Option_Ft ( 4) := 'SIZE';
 15     Tabtype_Config_Options.EXTEND;
 16     Tabtype_Config_Options ( 1) := 'RED';
 17     Tabtype_Config_Options.EXTEND;
 18     Tabtype_Config_Options ( 2) := '1.23';
 19     Tabtype_Config_Options.EXTEND;
 20     Tabtype_Config_Options ( 3) := '4.56';
 21     Tabtype_Config_Options.EXTEND;
 22     Tabtype_Config_Options ( 4) := '13';
 23
 24     FOR I IN 1 .. Tabtype_Config_Option_Ft.COUNT
 25     LOOP
 26        DBMS_OUTPUT.Put ( Tabtype_Config_Option_Ft ( I) || CHR ( 9));
 27     END LOOP;
 28
 29     DBMS_OUTPUT.New_Line;
 30
 31     FOR I IN 1 .. Tabtype_Config_Option_Ft.COUNT
 32     LOOP
 33        DBMS_OUTPUT.Put ( Tabtype_Config_Options ( I) || CHR ( 9));
 34     END LOOP;
 35
 36     DBMS_OUTPUT.New_Line;
 37  END;
 38  /
COLOR   WIDTH   LENGTH  SIZE
RED     1.23    4.56    13

PL/SQL procedure successfully completed.

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Subquery in Oracle: Sub queries are one of advance queries in oracle. Types of advance queries: •      Sub Queries •      Hierarchical Queries •      Set Operators Sub queries are know as the query called from another query or another subquery. It can …
How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now