Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Transpose columns into rows

Posted on 2004-08-06
14
Medium Priority
?
8,569 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
How much do you know about the future of data centers? If you're like 50% of organizations, then it's probably not enough. Read on to get up to speed on this emerging field.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

564 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