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
764 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 69

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
powershell to dynamically populate dropdown list with selection in list box 2 39
SQL Exceptions 3 37
grouping logic 6 47
SQL Select Statement 2 21
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now