?
Solved

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
3
Medium Priority
?
777 Views
Last Modified: 2014-04-03
I have two views

vw_apps

vw_app_owners

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      

owners
Bob
Sue

for app2

owners
Jim
sally

for app3
Cindy
Vicki
Mike


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
0
Comment
Question by:Bastyon
3 Comments
 
LVL 6

Expert Comment

by:Dulton
ID: 39975766
try this on.
;with AppOwners AS

(

        SELECT [owner]

                  ,[app_pk]

                  ,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

UNION ALL

        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]

          ,ol.[Owners]

  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

0
 
LVL 70

Accepted Solution

by:
É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
0
 
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

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

840 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