Unix Technical Forum

Transpose rows into columns

This is a discussion on Transpose rows into columns within the Sybase forums, part of the Database Server Software category; --> Hi, I have a table of this format.. id, value1, value2, rate 1 1 2M 2 2 1 3M ...


Go Back   Unix Technical Forum > Database Server Software > Sybase

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-08-2008, 06:07 PM
dufffman@gmail.com
 
Posts: n/a
Default Transpose rows into columns

Hi,

I have a table of this format..

id, value1, value2, rate
1 1 2M 2
2 1 3M 33
3 1 4M 44
4 2 1M 111
5 2 2M 222
6 2 3M 333

I want to have a table that looks like this

id value1 1M 2M 3M 4M total
1 1 0 2 33 44 79
2 1 111 222 333 0 366


Any suggestions?

Note: the numer of M's is constant, not dynamic.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-08-2008, 06:07 PM
DBAGAL
 
Posts: n/a
Default Re: Transpose rows into columns

Hi!!

I think something like this will work:

select
value1,
'1M'=sum(1*charindex('1M',value2)*rate),
'2M'=sum(1*charindex('2M',value2)*rate),
'3M'=sum(1*charindex('3M',value2)*rate),
'4M'=sum(1*charindex('4M',value2)*rate),
'Total'=sum(rate)
from t1
group by value1

I don't think you need the "id" column in your result set ...

Good Luck!

Sara ...

dufffman@gmail.com wrote:
> Hi,
>
> I have a table of this format..
>
> id, value1, value2, rate
> 1 1 2M 2
> 2 1 3M 33
> 3 1 4M 44
> 4 2 1M 111
> 5 2 2M 222
> 6 2 3M 333
>
> I want to have a table that looks like this
>
> id value1 1M 2M 3M 4M total
> 1 1 0 2 33 44 79
> 2 1 111 222 333 0 366
>
>
> Any suggestions?
>
> Note: the numer of M's is constant, not dynamic.


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 02:25 AM.


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