This question already has an answer here:
- Oracle SQL pivot query 2 answers
I need to write a query which takes rows and converts it into columns - here's my table:
Count fname lname id
-----------------------------
1 abc def 20
2 pqr 20
3 abc xyz 20
4 xyz xyz 20
1 abc def 21
1 pqr xyz 22
2 abc abc 22
This is the output I'm trying to produce:
id fname lname fname lname fname lname fname lname
-------------------------------------------------------------
20 abc def pqr NULL abc xyz xyz xyz
21 abc def NULL NULL NULL NULL NULL NULL
22 abc abc NULL NULL NULL NULL NULL NULL
The max value of count for each id is 4. I'm using Oracle 9i.
I know you're after an Oracle 9i solution, but Oracle 11 introduces PIVOT, which allows you to do queries like:
which gives:
Not quite what you were after, but extremely useful in many circumstances.... and almost worth the upgrade for PIVOT and UNPIVOT alone
EDIT
Modified to put fname and lname in separate columns
Is this what you're looking for?
http://bytes.com/topic/sql-server/answers/531936-convert-rows-into-columns
Look at this example, same principle as in @Mike M. answer, but with true Oracle realization:
Here's another one you might have some luck with. I like @ThinkJet's but not sure how much decode costs (if more or less than this below.