Solved

Transpose columns into rows

Posted on 2004-08-06
14
8,526 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:SarikaKev
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 12

Expert Comment

by:catchmeifuwant
Comment Utility
Did it work?

If it worked why a Grade "B"?
0
 

Author Comment

by:SarikaKev
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility

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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article describes some very basic things about SQL Server filegroups.
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now