dbTalk Databases Forums  

A transposition Problem

comp.databases.sybase comp.databases.sybase


Discuss A transposition Problem in the comp.databases.sybase forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
David GIOT
 
Posts: n/a

Default A transposition Problem - 05-24-2006 , 06:59 AM






Hi,
i would like to transpose a table (3 columns, 100 rows) to (30 columns
, 100 rows) i.e:

ID kw_name kw_value
1 NAME a1
1 NUM b1
2 NAME a2
2 NUM b2
3 NAME a3
3 NUM b3
....

TO:

ID NAME NUM
1 a1 b1
2 a2 b2
3 a3 b3
....

Basically the first table is in a warehouse, and the wanted result will
be a view from the first table.

I've tried :

select ID,
case kw_name when 'NAME' then kw_value end as NAME,
case kw_name when 'NUM' then kw_value end as NUM,
from table group by ID,kw_value,kw_name

but i have a wrong result:
ID NAME NUM
1 null null
1 a1 null
1 null b1
2 null null
2 a2 null
2 null b2
....


Do you have an idea ?


Reply With Quote
  #2  
Old   
DBAGAL
 
Posts: n/a

Default Re: A transposition Problem - 05-24-2006 , 05:42 PM






Hi David,

Try the following:

Create table t1 (ID int, kw_name char(03), kw_value char(02)) -- My
version of your table.

select ID,
"NAME"=max(substring(kw_value,1,3*(charindex('NAM' ,kw_name)))),

"NUM"=max(substring(kw_value,1,2*(charindex('NUM', kw_name))))
from t1
group by ID
go

ID NAME NUM
----------- ---- ---
1 a1 b1
2 a2 b2
3 a3 b3

(3 rows affected)

For variable length columns substitute 'datalength(kw_value)' for the
hard-coded '2' and '3' above ...

I think this table pivot is what you are looking for ...

Good Luck,

Sara ...


Reply With Quote
  #3  
Old   
David GIOT
 
Posts: n/a

Default Re: A transposition Problem - 05-26-2006 , 04:03 AM



Dear Sara,

Thank you a lot ,
it works !

David.


DBAGAL wrote:
Quote:
Hi David,

Try the following:

Create table t1 (ID int, kw_name char(03), kw_value char(02)) -- My
version of your table.

select ID,
"NAME"=max(substring(kw_value,1,3*(charindex('NAM' ,kw_name)))),

"NUM"=max(substring(kw_value,1,2*(charindex('NUM', kw_name))))
from t1
group by ID
go

ID NAME NUM
----------- ---- ---
1 a1 b1
2 a2 b2
3 a3 b3

(3 rows affected)

For variable length columns substitute 'datalength(kw_value)' for the
hard-coded '2' and '3' above ...

I think this table pivot is what you are looking for ...

Good Luck,

Sara ...


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



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.