Transpose columns into rows

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
SarikaKevAsked:
Who is Participating?
 
catchmeifuwantConnect With a Mentor Commented:
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
 
catchmeifuwantCommented:
what database are you using ? Here's one on Oracle..

http://www.experts-exchange.com/Databases/Oracle/Q_20834788.html
0
 
SarikaKevAuthor Commented:
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
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
catchmeifuwantCommented:
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
 
SarikaKevAuthor Commented:
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
 
catchmeifuwantCommented:
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
 
SarikaKevAuthor Commented:
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
 
catchmeifuwantCommented:
Did it work?

If it worked why a Grade "B"?
0
 
SarikaKevAuthor Commented:
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
 
catchmeifuwantCommented:
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
 
SarikaKevAuthor Commented:
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
 
catchmeifuwantCommented:
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
 
SarikaKevAuthor Commented:

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
 
catchmeifuwantCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.