Home » SQL & PL/SQL » SQL & PL/SQL » Varray to Columns with defaults
Varray to Columns with defaults [message #634109] Wed, 04 March 2015 09:20 Go to next message
bharathram
Messages: 1
Registered: March 2015
Junior Member
Hi
Wanted to check as how we can convert varray to columns

Student scores
----------------------
1 varray(10,20,30)
2 varray(11,22)
3 varray(31)

Was looking at a output as 1,10,20,30
2,11,22,0
3,31,0,0
Re: Varray to Columns with defaults [message #634111 is a reply to message #634109] Wed, 04 March 2015 10:28 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Welcome to the forum.

Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.
Also always post your Oracle version, with 4 decimals.

With any SQL or PL/SQL question, please, Post a working Test case: create table (including all constraints) and insert statements along with the result you want with these data then we will be able work with your table and data. Explain with words and sentences the rules that lead to this result.

Re: Varray to Columns with defaults [message #634114 is a reply to message #634111] Wed, 04 March 2015 10:43 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Here's a way how you should post the test case:
create or replace type vtyp as varray(10) of number;
/
drop table t;
create  table t (id integer, val vtyp);
insert into t values (1, vtyp(10,20,30));
insert into t values (2, vtyp(11,22));
insert into t values (3, vtyp(31));
commit;

Here's a starter of solution:
SQL> col val format a30
SQL> select * from t;
        ID VAL
---------- ------------------------------
         1 VTYP(10, 20, 30)
         2 VTYP(11, 22)
         3 VTYP(31)

3 rows selected.

SQL> select id, x.column_value elem
  2  from t, table(val) x
  3  /
        ID       ELEM
---------- ----------
         1         10
         1         20
         1         30
         2         11
         2         22
         3         31

6 rows selected.

The next step depends on your version.
Re: Varray to Columns with defaults [message #634115 is a reply to message #634109] Wed, 04 March 2015 10:45 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
This results in variable number of columns. You would need to use something like ODCI interface. Check tis forum - there was a discussion splitting string into variable number of columns and ODCI interface as possible solution. Another possibility is dynamic SQL.

SY.
Re: Varray to Columns with defaults [message #634116 is a reply to message #634115] Wed, 04 March 2015 10:48 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

I don't know if the result is several columns or just a single column with comma separated values; looking at the input display (2 columns), I tend for the latter.

Re: Varray to Columns with defaults [message #634118 is a reply to message #634116] Wed, 04 March 2015 11:07 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
Well, topic is Varray to Columns with defaults, right Laughing

SY.
Re: Varray to Columns with defaults [message #634119 is a reply to message #634118] Wed, 04 March 2015 11:09 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Laughing

Re: Varray to Columns with defaults [message #634120 is a reply to message #634118] Wed, 04 March 2015 11:22 Go to previous message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

So knowing a VARRAY has a fixed maximum number of elements, the query is static (rebuilding my type with 3 elements in the varray):
SQL> with 
  2    data as (
  3      select id, x.column_value elem,
  4             row_number() over (partition by id order by null) rn
  5      from t, table(val) x
  6    )
  7  select id,
  8         nvl(max(decode(rn, 1, elem)),0) v1,
  9         nvl(max(decode(rn, 2, elem)),0) v2,
 10         nvl(max(decode(rn, 3, elem)),0) v3
 11  from data
 12  group by id
 13  order by id
 14  /
        ID         V1         V2         V3
---------- ---------- ---------- ----------
         1         10         20         30
         2         11         22          0
         3         31          0          0

3 rows selected.

Previous Topic: Regexp_Substr
Next Topic: complete refresh of materialized view works fine, but non-complete doesn't
Goto Forum:
  


Current Time: Fri Apr 26 07:36:42 CDT 2024