Home » SQL & PL/SQL » SQL & PL/SQL » Convert rows into columns (oracle 10g,win xp)
Convert rows into columns [message #607959] Thu, 13 February 2014 22:43 Go to next message
Xandot
Messages: 235
Registered: January 2014
Location: India
Senior Member
hi friends ,
Please help me out ....

my Query:--
with t as
(select 'abc,efg,ijk' a from dual
union all
select '12,45,78' from dual)
select * from t;

o/p:--
col1 ----- col2
-------------------------------
abc ----- 12
efg ----- 45
ijk ----- 78

Thanks.....

[Updated on: Thu, 13 February 2014 22:54]

Report message to a moderator

Re: Convert rows into columns [message #607975 is a reply to message #607959] Fri, 14 February 2014 00:37 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

It is a standard "pivot query" once you have "split" the strings, please do search on these.

[Updated on: Fri, 14 February 2014 00:44]

Report message to a moderator

Re: Convert rows into columns [message #607980 is a reply to message #607975] Fri, 14 February 2014 00:52 Go to previous messageGo to next message
Xandot
Messages: 235
Registered: January 2014
Location: India
Senior Member
I AM USING 10 G WHERE I CANT USE PIVOT
Re: Convert rows into columns [message #607983 is a reply to message #607980] Fri, 14 February 2014 01:03 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

"Pivot" is a& method to write SQL that existed long before 11g introduced PIVOT feature.
And don't post in UPPER case which means you are shouting.

[Updated on: Fri, 14 February 2014 01:04]

Report message to a moderator

Re: Convert rows into columns [message #607987 is a reply to message #607983] Fri, 14 February 2014 01:14 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

SQL> with 
  2    t as ( 
  3     select 'abc,efg,ijk' a from dual
  4     union all
  5     select '12,45,78' from dual
  6    ),
  7    data as ( select ','||a||',' val, rownum coln from t ),
  8    split as (
  9      select substr(regexp_substr(val, ',[^,]*', 1, column_value),2) item, 
 10             column_value rown, coln
 11      from data,
 12           table(cast(multiset(select level from dual 
 13                               connect by level <
 14                                          length(val)-length(replace(val,',','')))
 15                      as sys.odciNumberList))
 16    )
 17  select max(decode(coln,1,item)) col1,
 18         max(decode(coln,2,item)) col2
 19  from split
 20  group by rown
 21  order by rown
 22  /
COL1         COL2
------------ ------------
abc          12
efg          45
ijk          78

3 rows selected.

Re: Convert rows into columns [message #607989 is a reply to message #607987] Fri, 14 February 2014 01:21 Go to previous messageGo to next message
Xandot
Messages: 235
Registered: January 2014
Location: India
Senior Member
Is there any other via to do the same ....



Thanks you so much.....Smile

Re: Convert rows into columns [message #607992 is a reply to message #607989] Fri, 14 February 2014 01:33 Go to previous message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Sure there are other ways but with the same kind of methods as there are several ways to split and several ways to pivot.
I'm sure others will post some of them.

Previous Topic: converting clob to varchar2
Next Topic: query help
Goto Forum:
  


Current Time: Fri Apr 26 03:19:57 CDT 2024