Geeks Talk

Prepare for your Next Interview




How do I transpose Rows into Columns?

This is a discussion on How do I transpose Rows into Columns? within the SQL forums, part of the Databases category; How do I transpose Rows into Columns? For example, if I've the data as follows: Name Value ==== ==== Name1 V11 Name1 V12 Name1 V13 Name2 V21 Name2 V22 I want ...


Go Back   Geeks Talk > Databases > SQL

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 06-19-2007
Junior Member
 
Join Date: Mar 2007
Posts: 2
Thanks: 0
Thanked 1 Time in 1 Post
Anasuya1031 is on a distinguished road
How do I transpose Rows into Columns?

How do I transpose Rows into Columns?

For example, if I've the data as follows:

Name Value
==== ====
Name1 V11
Name1 V12
Name1 V13
Name2 V21
Name2 V22

I want the OUTPUT to be as follows:

Name1 V11 V12 V13
Name2 V21 V22

Apperciate if anyone can help me out in this.

Waiting for your reply,

Cheers,
Reply With Quote
The Following User Says Thank You to Anasuya1031 For This Useful Post:
Sponsored Links
  #2 (permalink)  
Old 07-09-2007
Contributing Member
 
Join Date: Apr 2006
Location: kolkata(now in noida)
Posts: 56
Thanks: 9
Thanked 3 Times in 2 Posts
bhaski is on a distinguished road
Re: How do I transpose Rows into Columns?

hi i did it with my table as yr req. try this in yr table----

(it's very easy)--


10:27:36 SQL> seelct *from test;
SP2-0734: unknown command beginning "seelct *fr..." - rest of line ignored.
10:47:40 SQL> select *from test;

C
-
a
a
a
a
a
b
b
b

8 rows selected.

Elapsed: 00:00:00.00

Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=3 Card=8 Bytes=16)
1 0 TABLE ACCESS (FULL) OF 'TEST' (TABLE) (Cost=3 Card=8 Bytes
=16)




10:47:48 SQL> select
10:47:57 2 max(decode(rn, 1, col)) col1,
10:47:57 3 max(decode(rn, 2, col)) col2,
10:47:57 4 max(decode(rn, 3, col)) col3,
10:47:57 5 max(decode(rn, 4, col)) col4,
10:47:57 6 max(decode(rn, 5, col)) col5
10:47:57 7 from (
10:47:57 8 select col, row_number() over (partition by col order by col) rn
10:47:57 9 from test
10:47:57 10 order by col)
10:47:57 11 group by col;

C C C C C
- - - - -
a a a a a
b b b

Elapsed: 00:00:00.00

Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT Optimizer=CHOOSE (Cost=3 Card=2 Bytes=30)
1 0 SORT (GROUP BY NOSORT) (Cost=3 Card=2 Bytes=30)
2 1 VIEW (Cost=3 Card=8 Bytes=120)
3 2 WINDOW (SORT) (Cost=3 Card=8 Bytes=16)
4 3 TABLE ACCESS (FULL) OF 'TEST' (TABLE) (Cost=3 Card=8
Bytes=16)
Reply With Quote
  #3 (permalink)  
Old 07-09-2007
Moderator
 
Join Date: Jun 2007
Location: Bangalore,India
Posts: 1,432
Thanks: 8
Thanked 126 Times in 113 Posts
debasisdas will become famous soon enoughdebasisdas will become famous soon enough
Re: How do I transpose Rows into Columns?

try writing some matrix query using decode as shown in sample of previous example.
Reply With Quote
Reply

  Geeks Talk > Databases > SQL


Thread Tools
Display Modes


Similar Threads

Thread Thread Starter Forum Replies Last Post
How to add values to other columns Mohamed_Rafi Oracle 10 03-27-2008 12:35 AM
In my source table I have two columns Geek_Guest Data Warehousing 3 03-26-2008 04:20 AM
Joining Tables and mapping columns JobHelper SQL 1 08-20-2007 02:30 PM
How to know all the mandatory columns that need to be filled Geek_Guest Oracle Apps 0 05-13-2007 05:36 AM
transpose datagrid or gridview ravi0207 ASP.NET 3 10-26-2006 08:58 AM


All times are GMT -4. The time now is 01:52 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Copyright © 2008 GeekInterview.com. All Rights Reserved