Unix Technical Forum

Transpose rows to column

This is a discussion on Transpose rows to column within the DB2 forums, part of the Database Server Software category; --> Hi I have a table with data like this ID Type 1 A 1 B 1 C 2 A ...


Go Back   Unix Technical Forum > Database Server Software > DB2

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-27-2008, 02:11 AM
Tuong Do
 
Posts: n/a
Default Transpose rows to column

Hi

I have a table with data like this

ID Type
1 A
1 B
1 C
2 A
2 C


How can I tranfer it to this format
ID Type1 Type2 Type3
1 A B C
2 A Null C





Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-27-2008, 02:11 AM
Mark Yudkin
 
Posts: n/a
Default Re: Transpose rows to column

Almost all report writers offer cross-tabulation.

Standard SQL cannot solve the general case (unknown number of columns),
although the cube functionality will do the tabulation for you. However,
have to do the cross-tabulation layout yourself.

"Tuong Do" <tuongdo@hotmail.com> wrote in message
news:cmcgbd$kmg$1@news-02.connect.com.au...
> Hi
>
> I have a table with data like this
>
> ID Type
> 1 A
> 1 B
> 1 C
> 2 A
> 2 C
>
>
> How can I tranfer it to this format
> ID Type1 Type2 Type3
> 1 A B C
> 2 A Null C
>
>
>
>
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-27-2008, 02:11 AM
dariuscooper@gmail.com
 
Posts: n/a
Default Re: Transpose rows to column

try the Case statement...something like this

select ID,
min(case type='A' then 'A' else null) type_A
min(case type='B' then 'B' else null) type_B
group by ID

Tuong Do wrote:
> Hi
>
> I have a table with data like this
>
> ID Type
> 1 A
> 1 B
> 1 C
> 2 A
> 2 C
>
>
> How can I tranfer it to this format
> ID Type1 Type2 Type3
> 1 A B C
> 2 A Null C


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-27-2008, 02:11 AM
Tuong Do
 
Posts: n/a
Default Re: Transpose rows to column

Thank for the quick reply,
I will try the create a view with the case statement

<dariuscooper@gmail.com> wrote in message
news:1099597298.559924.4900@c13g2000cwb.googlegrou ps.com...
> try the Case statement...something like this
>
> select ID,
> min(case type='A' then 'A' else null) type_A
> min(case type='B' then 'B' else null) type_B
> group by ID
>
> Tuong Do wrote:
>> Hi
>>
>> I have a table with data like this
>>
>> ID Type
>> 1 A
>> 1 B
>> 1 C
>> 2 A
>> 2 C
>>
>>
>> How can I tranfer it to this format
>> ID Type1 Type2 Type3
>> 1 A B C
>> 2 A Null C

>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-27-2008, 02:19 AM
Tokunaga T.
 
Posts: n/a
Default Re: Transpose rows to column

A little generalized way.
You do not need to know values of Type beforehand.
But, still need to set maximum number of Types.

WITH Types AS (
SELECT Type
, ROWNUMBER() OVER(ORDER BY Type) AS rn
FROM (SELECT DISTINCT Type
FROM Transpose
) AS R
)
SELECT ID
, MIN(CASE WHEN rn = 1 THEN a.type END) type1
, MIN(CASE WHEN rn = 2 THEN a.type END) type2
, MIN(CASE WHEN rn = 3 THEN a.type END) type3
, MIN(CASE WHEN rn = 4 THEN a.type END) type4
, MIN(CASE WHEN rn = 5 THEN a.type END) type5
FROM Transpose A
, Types T
WHERE a.type = t.type
GROUP BY
ID
;
------------------------------------------------------------------------------

ID TYPE1 TYPE2 TYPE3 TYPE4 TYPE5
----------- ----- ----- ----- ----- -----
1 A B C - -
2 A - C - -

2 record(s) selected.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 05:08 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com