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
769 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 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

786 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