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 |
|
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 #634114 is a reply to message #634111] |
Wed, 04 March 2015 10:43 |
|
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 |
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 #634120 is a reply to message #634118] |
Wed, 04 March 2015 11:22 |
|
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.
|
|
|
Goto Forum:
Current Time: Fri Apr 26 07:36:42 CDT 2024
|