Unix Technical Forum

concatenating multiple rows

This is a discussion on concatenating multiple rows within the Sybase forums, part of the Database Server Software category; --> Hi all, I am trying to combine multiple rows value of a column into one. For example, col1 col2 ...


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, 04:03 PM
santosh94538@yahoo.com
 
Posts: n/a
Default concatenating multiple rows

Hi all,
I am trying to combine multiple rows value of a column into one. For example,

col1 col2 col3 col4
1 a 5 b
1 a 3 c
1 a 7 d
2 a 8 e
2 a 9 x

the result I would like to see is

col1 col2 col3 col4
1 a 5,3,7 b,c,d
2 a 8,9 e,x

I would appreciate any help. Thanks in advance
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-08-2008, 04:03 PM
Ulrike Zintz
 
Posts: n/a
Default Re: concatenating multiple rows

Hi,

You could use a cursor on the query:

select col1, col2, col3, col4
from mytable
order by col1, col2

and then concatenate the col3 values as long as the rows have got the
same col1 values and the same col2 values.

The results can be inserted into a table, so you can use select * from
newtable to display the final results.

Hope that helps.

Regards,
Ulrike

santosh94538@yahoo.com wrote in message news:<2a368842.0310271024.366ea7b3@posting.google. com>...
> Hi all,
> I am trying to combine multiple rows value of a column into one. For example,
>
> col1 col2 col3 col4
> 1 a 5 b
> 1 a 3 c
> 1 a 7 d
> 2 a 8 e
> 2 a 9 x
>
> the result I would like to see is
>
> col1 col2 col3 col4
> 1 a 5,3,7 b,c,d
> 2 a 8,9 e,x
>
> I would appreciate any help. Thanks in advance

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-08-2008, 04:03 PM
Torrente
 
Posts: n/a
Default Re: concatenating multiple rows

Hi Santosh!

How about:

select col1, col2, list(col3), list(col4)
from mytable
group by col1

Regards, Neven

santosh94538@yahoo.com wrote:
> Hi all,
> I am trying to combine multiple rows value of a column into one. For
> example,
>
> col1 col2 col3 col4
> 1 a 5 b
> 1 a 3 c
> 1 a 7 d
> 2 a 8 e
> 2 a 9 x
>
> the result I would like to see is
>
> col1 col2 col3 col4
> 1 a 5,3,7 b,c,d
> 2 a 8,9 e,x
>
> I would appreciate any help. Thanks in advance




Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-08-2008, 04:03 PM
santosh94538@yahoo.com
 
Posts: n/a
Default Re: concatenating multiple rows

Thanks Ulrike for your help. Could you please tell me how to write it
out. I am new to SQL. Thanks

zintz@arcor.de (Ulrike Zintz) wrote in message news:<539a5d55.0310280036.446248@posting.google.co m>...
> Hi,
>
> You could use a cursor on the query:
>
> select col1, col2, col3, col4
> from mytable
> order by col1, col2
>
> and then concatenate the col3 values as long as the rows have got the
> same col1 values and the same col2 values.
>
> The results can be inserted into a table, so you can use select * from
> newtable to display the final results.
>
> Hope that helps.
>
> Regards,
> Ulrike
>
> santosh94538@yahoo.com wrote in message news:<2a368842.0310271024.366ea7b3@posting.google. com>...
> > Hi all,
> > I am trying to combine multiple rows value of a column into one. For example,
> >
> > col1 col2 col3 col4
> > 1 a 5 b
> > 1 a 3 c
> > 1 a 7 d
> > 2 a 8 e
> > 2 a 9 x
> >
> > the result I would like to see is
> >
> > col1 col2 col3 col4
> > 1 a 5,3,7 b,c,d
> > 2 a 8,9 e,x
> >
> > I would appreciate any help. Thanks in advance

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-08-2008, 04:03 PM
Karl Ritter
 
Posts: n/a
Default Re: concatenating multiple rows

You will find doc on T-SQL at

http://sybooks.sybase.com/asg1250e.html

Download doc

Transact-SQL User's Guide

Chapter 17 covers Cursors.

Karl


<santosh94538@yahoo.com> wrote in message
news:2a368842.0310291044.32e4eda1@posting.google.c om...
> Thanks Ulrike for your help. Could you please tell me how to write it
> out. I am new to SQL. Thanks
>
> zintz@arcor.de (Ulrike Zintz) wrote in message

news:<539a5d55.0310280036.446248@posting.google.co m>...
> > Hi,
> >
> > You could use a cursor on the query:
> >
> > select col1, col2, col3, col4
> > from mytable
> > order by col1, col2
> >
> > and then concatenate the col3 values as long as the rows have got the
> > same col1 values and the same col2 values.
> >
> > The results can be inserted into a table, so you can use select * from
> > newtable to display the final results.
> >
> > Hope that helps.
> >
> > Regards,
> > Ulrike
> >
> > santosh94538@yahoo.com wrote in message

news:<2a368842.0310271024.366ea7b3@posting.google. com>...
> > > Hi all,
> > > I am trying to combine multiple rows value of a column into one. For

example,
> > >
> > > col1 col2 col3 col4
> > > 1 a 5 b
> > > 1 a 3 c
> > > 1 a 7 d
> > > 2 a 8 e
> > > 2 a 9 x
> > >
> > > the result I would like to see is
> > >
> > > col1 col2 col3 col4
> > > 1 a 5,3,7 b,c,d
> > > 2 a 8,9 e,x
> > >
> > > I would appreciate any help. Thanks in advance



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 07:52 AM.


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