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
761 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
Comment Utility
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
Comment Utility
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
Comment Utility
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)

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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 utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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…

762 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

10 Experts available now in Live!

Get 1:1 Help Now