How do I concatenate the row values of one select statement into a columns row in a separate SQL statement.

Posted on 2014-04-03
Medium Priority
Last Modified: 2014-04-03
I have two views



For simplicity lets say that  the apps view returns

app_pk    app

1      app1
2      app2
3      app3

if I query the view app owners by app_pk (Select owner from vw_app_owner where app_pk = @app_pk)

I would get

for app1      


for app2


for app3

I want to with a single query return

app      app_owners

with the owners in a  single column concatenated for each app

so the results would be

app      owners
app1    Bob, Sue
app2    Jim, Sally
app3    Cindy, Vicki, Mike
Question by:Bastyon
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 39975766
try this on.
;with AppOwners AS


        SELECT [owner]


                  ,ROW_NUMBER() OVER(PARTITION BY app_pk  ORDER BY [OwnerOrder]

          FROM vw_app_owners

        WHERE [Owner]  IS NOT NULL      

), OwnerList AS


        SELECT Cast(ao.[owner] AS VARCHAR(Max)) AS [Owners]

                  ,Cast(ao.[app_pk] AS INT) AS [App_Pk]

                  ,Cast(ao.[OwnerOrder] AS INT) AS [OwnerOrder]

          FROM AppOwners  AS ao

        WHERE ao.[OwnerOrder] = 1


        SELECT Cast(ol.[Owners] + ', '  + ao.[owner] AS VARCHAR(Max))

                  ,Cast(ao.[App_pk] AS INT)

                  ,Cast(ao.[OwnerOrders] AS INT)

          FROM OwnerList  AS ol

INNER JOIN AppOwners  AS ao

                ON ol.app_pk = ao.app_pk

           AND ol.OwnerOrder + 1 =  ao.OwnerOrder




select va.[app]


  from OwnerList  as ol

  INNER JOIN  vw_apps as va

        on ol.app_pk = va.app_pk

WHERE EXISTS (select max(ao.OwnerOrder) 

                                 from appowners as  ao 

                                where ol.app_pk = ao.app_pk)

Open in new window

LVL 70

Accepted Solution

Éric Moreau earned 2000 total points
ID: 39975768
you have to use STUFF in SQL.

this short post will show you how: http://emoreau.com/Entries/Blogs/2009/10/SQL-Concatenate-values-multiple-from-multiple-lines.aspx
LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 39975786
I observed that you pass PK in where clause. In this case below one is sufficient.

DECLARE @List varchar(1000) 

Select @List = COALESCE(@List + ', ', '') + CAST(owner AS varchar(50))
from vw_app_owner
where app_pk = @app_pk

Select @List 

Open in new window


Featured Post

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question