Solved

Transpose columns into rows

Posted on 2004-08-06
14
8,554 Views
Last Modified: 2011-08-18
Good day

Can u please assist with the ffg:

I have a table that stores data in columns
adhoc eg
DATE  NAME      MARK1        MARK2       MARK3         MARK4  ........  MARK24
AUG    RIKA         45               77              33             27                     88

I need to insert this data into the student table from the above table
DATE   NAME  MARKS
AUG      RIKA   45
AUG      RIKA   77
AUG      RIKA   33
AUG      RIKA   27

how can i transpose the columns into rows for teh inserting table

please help. im using pl/sql

thanks alot
rika
0
Comment
Question by:SarikaKev
  • 8
  • 6
14 Comments
 
LVL 12

Expert Comment

by:catchmeifuwant
ID: 11733877
what database are you using ? Here's one on Oracle..

http://www.experts-exchange.com/Databases/Oracle/Q_20834788.html
0
 

Author Comment

by:SarikaKev
ID: 11734157
Hi .
im using oracle
but I have to transpose to rows
DATE     TEST   NAME  MARKS
AUG      TEST1   RIKA   45
AUG      TEST2   RIKA   77
AUG      TEST3   RIKA   33
AUG      TEST4   RIKA   27

EVERY ROW IS UNIQUE
THX
0
 
LVL 12

Expert Comment

by:catchmeifuwant
ID: 11734233
is this what you want ?

SELECT DT,NAME,MAX(DECODE(RK,1,MARKS,0)) AS MARK_1,MAX(DECODE(RK,2,MARKS,0)) AS MARK_2,MAX(DECODE(RK,3,MARKS,0)) AS MARK_3
FROM
(
SELECT DT,NAME,MARKS,ROW_NUMBER() OVER (PARTITION BY DT,NAME ORDER BY DT,NAME) AS RK
FROM TEMP
)
GROUP BY DT,NAME
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:SarikaKev
ID: 11734595
hi again
well im not sure what this does really.
is temp the table im inserting into
the fields in the two tables are different as well.
where do i specify the table name for the select from first table. ie TABLE A
DATE  NAME      MARK1        MARK2       MARK3         MARK4  ........  MARK24
AUG    RIKA         45               77              33             27                     88

TABLE B
DATE     TEST   STUDENT  RESULT  
AUG      TEST1     RIKA       45
AUG      TEST2     RIKA       77
AUG      TEST3     RIKA       33
AUG      TEST4     RIKA       27

I must apologise for my misunderstanding as i am new to pl/sql.
thanks alot
0
 
LVL 12

Expert Comment

by:catchmeifuwant
ID: 11734642
replace temp with your table name (B).

Post your table structure (the columns and the data type) and the data in your table...

that will be helpful
0
 

Author Comment

by:SarikaKev
ID: 11760106
ok here is my table structure
table 1 (data in 1 column) TAB1                                      table 2 (data in rows) TAB2
ID                 number(38)                                               DAY                   date
DAY              date                                                          HOUR                number(2)
HOUR1          varchar2(20)                                              HOURLY_DATA   NUMBER(15,3)
HOUR2          varchar2(20)                                              (the hour is the number of hours in the
......                                                                               day ie 24) (the hourly data stores the 24
HOUR24        varchar2(20)                                               values from table 1 HOUR1..24)

HOUR1..24 stores the value of the "electricity_meter" on every hour.
hence i have to transpose the 24 columns from table 1 into 24 rows in table 2
hope this gives a better understanding
thanks alot
rika

0
 
LVL 12

Accepted Solution

by:
catchmeifuwant earned 50 total points
ID: 11760426
You can transform the data using this query :

insert into TAB2(id,day,hour,hourly_data)
select id,day,01,hour1
from tab1
union all
select id,day,02,hour2
from tab1
union all
...
select id,day,24,hour24
from tab1

Note: If there's no "ID" column in Tab2 remove it from the insert query..

0
 
LVL 12

Expert Comment

by:catchmeifuwant
ID: 11760531
Did it work?

If it worked why a Grade "B"?
0
 

Author Comment

by:SarikaKev
ID: 11760705
hi there

well im still testing the query with test data
im sorry ... I didn't realise the criticality of an 'A' rating
I just thought the answer was good.
can i alter this grading if it works :)

thx
0
 
LVL 12

Expert Comment

by:catchmeifuwant
ID: 11760727
You need to post it in community support area  requesting the moderator to amend the rating

http://www.experts-exchange.com/Community_Support/

Thanks!
0
 

Author Comment

by:SarikaKev
ID: 11760917
Hi again :)

The query does teh job however it takes almost 2 minutes to complete the insert.
Is this the most effecient way?

i had a suggestion to use dynamic arrays and then populate the second table using the array!
do u know if this is possible?

thanks for your help.
rika
0
 
LVL 12

Expert Comment

by:catchmeifuwant
ID: 11760928
How many records are there...response time is determined on how much data you have and how long you are willing to wait..

I do not think dynamic array is necessary here...

the idea is-  if you can keep it simple keep it simple :-)

Cheers!
0
 

Author Comment

by:SarikaKev
ID: 11760997

I have 24 records with a total of 72 fields in table 2
so each row has 3 fields that i have to populate (3col *24rows)

so my inserted table has
DAY, HOUR, HOURLY_DATA, HOURLY_DATA2, HOURLY_DATA3  
hence hourly data * 3 * 24 hours
in my example i just specified 1 hourly_data field.
so i add onto the fields in the query u gave.
so is this the best way then?


0
 
LVL 12

Expert Comment

by:catchmeifuwant
ID: 11761087
I cannot say this is the best way...I do not have any visibility to the data you are using...but if it is working fine for you and with the least amount of time taken...why not use it ?
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

APEX (Application Express) is used to develop a web application from Oracle. SQL Workshop is one of the tools that comes with Oracle APEX to query or modify the database objects or to make any changes to the structure.
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

792 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