Home All Groups Group Topic Archive Search About

concatenate column from rows

Author
2 Sep 2006 10:18 PM
Dahab
Hi,
Can anyone help me with this query.

I have a table GassBottleSize with colums Gas, Bottlesize, id and values

ID      Gas                     BottleSize
------------------------------------
1        Trimix1845            50
2        Trimix1845            100
3        Trimix1170            50
4        Ean32                    100

Output I want is:

Gas                    BottleSize
----------------------------
Trimix1845        50,100
Trimix1170        50
Ean32                100

Thanks
D.

Author
2 Sep 2006 8:12 PM
Steve Dassin
Author
2 Sep 2006 11:00 PM
Anith Sen
The ideal approach should be to retrieve the resultset to the client side
and format the results appropriately. To do such stuff in the server
reliably, you'll have to write some "complex" code. Some ideas can be found
at:
http://www.projectdmx.com/tsql/rowconcatenate.aspx

--
Anith
Author
4 Sep 2006 8:49 PM
Tony Rogerson
No, if you are using SQL 2005 just use FOR XML - its a piece of cake and the
benefit is you can keep it in the server and only a few lines of code.

--
Tony Rogerson
SQL Server MVP
http://sqlblogcasts.com/blogs/tonyrogerson - technical commentary from a SQL
Server Consultant
http://sqlserverfaq.com - free video tutorials


Show quote
"Anith Sen" <an***@bizdatasolutions.com> wrote in message
news:OtSTKNuzGHA.4452@TK2MSFTNGP05.phx.gbl...
> The ideal approach should be to retrieve the resultset to the client side
> and format the results appropriately. To do such stuff in the server
> reliably, you'll have to write some "complex" code. Some ideas can be
> found at:
> http://www.projectdmx.com/tsql/rowconcatenate.aspx
>
> --
> Anith
>
Author
5 Sep 2006 2:26 PM
Anith Sen
Did you get a chance to look at the various options described in that url?
The for xml trick is already suggested there.

--
Anith
Author
4 Sep 2006 8:49 PM
Tony Rogerson
If you are using SQL Server 2005 you can use the FOR XML extensions, I
discus it here:
http://sqlblogcasts.com/blogs/tonyrogerson/archive/2006/05/11/429.aspx

Example


create table mailing_list (
   individual_name nvarchar(100) not null,
   list_name nvarchar(10) not null
)

insert mailing_list ( individual_name, list_name ) values( 'tony r', 'List
A' )
insert mailing_list ( individual_name, list_name ) values( 'tony r', 'List
B' )
insert mailing_list ( individual_name, list_name ) values( 'tony r', 'List
C' )
insert mailing_list ( individual_name, list_name ) values( 'joe r', 'List
A' )
insert mailing_list ( individual_name, list_name ) values( 'joe r', 'List
B' )
insert mailing_list ( individual_name, list_name ) values( 'alex r', 'List
A' )

select distinct
   individual_name,
   list = substring(
      ( select ', ' + list_name as [text()]
         from mailing_list m2
         where m2.individual_name = m1.individual_name
         for xml path(''), elements )
      , 3, 100 )
from mailing_list m1



Gives this result :-

alex r      List A

joe r       List A, List B

tony r      List A, List B, List C


--
Tony Rogerson
SQL Server MVP
http://sqlblogcasts.com/blogs/tonyrogerson - technical commentary from a SQL
Server Consultant
http://sqlserverfaq.com - free video tutorials


Show quote
"Dahab" <trimix1***@hotmail.com> wrote in message
news:%23Xgm%232tzGHA.4452@TK2MSFTNGP05.phx.gbl...
> Hi,
> Can anyone help me with this query.
>
> I have a table GassBottleSize with colums Gas, Bottlesize, id and values
>
> ID      Gas                     BottleSize
> ------------------------------------
> 1        Trimix1845            50
> 2        Trimix1845            100
> 3        Trimix1170            50
> 4        Ean32                    100
>
> Output I want is:
>
> Gas                    BottleSize
> ----------------------------
> Trimix1845        50,100
> Trimix1170        50
> Ean32                100
>
> Thanks
> D.
>

AddThis Social Bookmark Button