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

x
?
Solved

Transpose columns into rows

Posted on 2004-08-06
14
Medium Priority
?
8,568 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
[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
  • 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
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.

 

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 150 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Suggested Courses

609 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