Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

sql - oracle (row to column)

Posted on 2009-04-01
5
Medium Priority
?
1,089 Views
Last Modified: 2013-12-19
I have the data something like this.

col_1     col_2  
x           abc
x           def

y           123
y           456




I need the o/p like this:

col_1    col_2    col_3

x          abc       def

y         123        456

0
Comment
Question by:vishali_vishu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 1

Author Comment

by:vishali_vishu
ID: 24043051

select x.col_1,x.col_2,y.col_2  from table x, table y
where x.col_1=y.col_1
and x.rowid <> y.rowid.

but i am getting o/p as:

col_1   col_2   col_3
x         abc      def
x         def      abc

y        123    456
y        456    123


0
 
LVL 74

Expert Comment

by:sdstuber
ID: 24043473
try this...
ee.txt
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 24043509
sample script to test it...

 
CREATE TABLE your_table
AS
    SELECT 'x' col_1, 'abc' col_2 FROM DUAL
    UNION ALL
    SELECT 'x', 'def' FROM DUAL
    UNION ALL
    SELECT 'y', '123' FROM DUAL
    UNION ALL
    SELECT 'y', '456' FROM DUAL;
 
  SELECT col_1, MAX(DECODE(rn, 1, col_2)) col_2, MAX(DECODE(rn, 2, col_2)) col_3
    FROM (SELECT col_1, col_2, ROW_NUMBER() OVER (PARTITION BY col_1 ORDER BY col_2) rn
            FROM your_table)
GROUP BY col_1;
 
 
Table created.
 
C COL COL
- --- ---
x abc def
y 123 456
 
2 rows selected.

Open in new window

0
 
LVL 1

Author Comment

by:vishali_vishu
ID: 24043689
sdstuber:

It is working good.

can you explain me bit further....


0
 
LVL 74

Accepted Solution

by:
sdstuber earned 2000 total points
ID: 24043742
anytime you have a query like this you don't understand, break it apart from the inside out...


SELECT col_1, col_2, ROW_NUMBER() OVER (PARTITION BY col_1 ORDER BY col_2) rn
            FROM your_table

this numbers each row by column 1,  so the first X is rn=1,  the second x is rn=2,  the first Y is rn=1, the second y is rn=2

then you select that data using decode to only keep the data for the "rn" you want for that column.


max and group by put it all together, remove that aggregation to see the raw results...

SELECT col_1, DECODE(rn, 1, col_2) col_2, DECODE(rn, 2, col_2) col_3
    FROM (SELECT col_1, col_2, ROW_NUMBER() OVER (PARTITION BY col_1 ORDER BY col_2) rn
            FROM your_table)

If you had more values for each x or y you could simply add more max and decodes...
 MAX(DECODE(rn, 3, col_2)) col_4, MAX(DECODE(rn, 4, col_2)) col_5, etc...
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
I showed you how to use console view (HERE (http://www.experts-exchange.com/articles/18379/Getting-Started-and-Using-the-Salesforce-com-Console.html)) -– but how do you set it up on the admin side of Salesforce? Note that you have to have Admin leve…
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

618 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