Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
Database Servers
DB2InformixIngresMS SQLOraclePervasive.SQLPostgreSQLProgressSybase
Desktop Databases
FileMakerFoxProMS AccessParadox
General
General DB TopicsDatabase Theory
Related Topics
Java Development.NET DevelopmentVB DevelopmentMore Topics ...

Database Forum / Oracle / Oracle Server / August 2007

Tip: Looking for answers? Try searching our database.

How to: Convert vertical table into horizontal table

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
tvjoshi - 30 Aug 2007 20:29 GMT
Hello friends,

I have 2 tables. One store employee information attributes as columns
of table in horizontal form.
Other store same employee's more information in a vertical form, in a
key-value pair. Let me explain by example.

TBL_EMPLOYEE
=========
Employee_id (int, pk) | EmployeeName (varchar) | Email (varchar)
       1                      | James                            |
james@gmail.com
       2                      | Karen                             |
karen@gmail.com
       3                      | peter                              |
peter@gmail.com

TBL_EMPLOYEE_METADATA
=================
Employee_id (int, pk, fk) | Property_Cd (varchar) |
Property_value(varchar)
        1                         | background_info          | james
is nice guy.
        1                         | special_info                 |
James has special info
        1                         | field_info                     |
James works in abc field
        2                         | background_info          | Karen
is nice girl
        2                         | special_info                 |
Karen has no special ifo
        2                         | field_info                     |
she does not work in field

The reason for other table is, these attributes may be different and
will be decidedly dynamically for each employee.

now while displaying data, I need to show employee data from both
tables in a single grid in following way:

EmployeeId | EmployeeName | Email                    |
background_info   | special_info
       1        | James              | james@gmail.com | james is
nice guy. | james has special info
       2        | karen               | karen@gmail.com  | karen is
nice girl.  | karen has no special ifo

So essentially, i need to transpose information in other table, so
that it can be shown horizontally with data in the first table.

There are 2 ways I can think of, to achieve this -
1. Do it on front end code by fetching both tables and then loop
through the other table to selectively add columns and data to first
table.
2. write a stored procedure, fetch data from 2nd table and dynamically
create a horizontal temp table out of it and then join with first
table and return data.

Has anyone done anything like this before? Is there any better, easier
way to do this, on database side?

Thanks a ton in advance,
tvjoshi
tvjoshi - 30 Aug 2007 20:40 GMT
Sorry guys.. table formatting got messed up. This is another attempt
to post same question so that formatting is proper.

Hello friends,

I have 2 tables. One store employee information attributes as columns
of table in horizontal form.
Other store same employee's more information in a vertical form, in a
key-value pair. Let me explain by example.

TBL_EMPLOYEE
=========
Employee_id (int, pk)
EmployeeName (varchar)
Email (varchar)

Employee_id | Name | Email
       1           | James | gmail.com
       2           | Karen | gmail.com
       3           | peter  | gmail.com

TBL_EMPLOYEE_METADATA
=================
Employee_id (int, pk, fk)
Property_Cd (varchar)
Property_value(varchar)

Employee_id | Property_Cd        | Property_value
        1          | backgrnd_info      | nice guy.
        1          | special_info          | special info
        1          | field_info              | in abc field
        2          | background_info  | nice girl
        2          | special_info          | no special ifo
        2          | field_info              | no work in field

The reason for other table is, these attributes may be different and
will be decidedly dynamically for each employee.

now while displaying data, I need to show employee data from both
tables in a single grid in following way:

EmployeeId | Name | Email         | backgrnd_info | special_info
       1         | James | gmail.com  | nice guy.         | special
info
       2         | karen  | gmail.com  | nice girl.          | no
special ifo

So essentially, i need to transpose information in other table, so
that it can be shown horizontally with data in the first table.

There are 2 ways I can think of, to achieve this -
1. Do it on front end code by fetching both tables and then loop
through the other table to selectively add columns and data to first
table.
2. write a stored procedure, fetch data from 2nd table and dynamically
create a horizontal temp table out of it and then join with first
table and return data.

Has anyone done anything like this before? Is there any better, easier
way to do this, on database side?

Thanks a ton in advance,
tvjoshi

> Hello friends,
>
[quoted text clipped - 59 lines]
> Thanks a ton in advance,
> tvjoshi
fitzjarrell@cox.net - 30 Aug 2007 21:26 GMT
> Sorry guys.. table formatting got messed up. This is another attempt
> to post same question so that formatting is proper.
[quoted text clipped - 125 lines]
>
> - Show quoted text -

One possibility:

with backgd_info as(
select employee_id, property_value
from tbl_employee_metadata
where property_cd = 'background_info'),
spec_info as(
select employee_id, property_value
from tbl_employee_metadata
where property_cd = 'special_info')
select e.employee_id, e.employeename, e.email,
      b.property_value background_info,
      s.property_value special_info
from tbl_employee e, backgd_info b, spec_info s
where b.employee_id = e.employee_id
and s.employee_id = e.employee_id
/

EMPLOYEE_ID EMPLOYEENA EMAIL
BACKGROUND_INFO                          SPECIAL_INFO
----------- ---------- --------------------
----------------------------------------
----------------------------------------
         1 James      gmail.com            nice
guy                                 special info
         2 Karen      gmail.com            nice
girl                                no special info

Others may provide additional 'solutions'.

David Fitzjarrell
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.