Solved

SQL query to create multiple values in a field when joining more than one table

Posted on 2011-03-16
4
254 Views
Last Modified: 2012-05-11
I need help to add mulitple values to a field in a  select query
I have joined 2 tables.

Table A

tableA_ID    nameid    Role
1                   1             1
1                  2             2
2                  3            1
2                  4            1
3                  5            1
3                  6            2
3                   7         3

Table B has values with foreign key(ID) from A like

tableA_ID Nameid realname
1                  1     john
1                    2     harry
3                    3     paul
3                  4     Dave
3                  5      Brooks


I need to create a select query joining table A with table B but I need a column in the results that joins realnames where the tableA_ID in Table B are the same.

So the results would show look like

tableA_ID   realname
1             john,harry
3             paul,dave,brooks
0
Comment
Question by:wademi
4 Comments
 
LVL 32

Accepted Solution

by:
ewangoya earned 500 total points
ID: 35152427
try
SELECT TableA_ID, Stuff(
                  (Select  ',' + RealName
                              From    TableB B
                              Where   A.TableA_ID = B.TableA_ID
                              For xml Path('')
                  )
                        
                  , 1, 1, '') as [Real Names]
                  
                        
From    TableA A
Group By TableA_ID

Open in new window

0
 

Expert Comment

by:BlakeRogers
ID: 35152457
This would work too I believe
SELECT TA.TableA_ID, TB.RealName
FROM TableA AS TA INNER JOIN TableB AS TB ON TA.TableA_ID = TB.TableA_ID
Group BY TA.TableA_ID

Open in new window

0
 
LVL 40

Expert Comment

by:Sharath
ID: 35153271
What is your SQL Server version?
0
 

Author Comment

by:wademi
ID: 35153532
Th version is SQL Server 2008
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

758 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

18 Experts available now in Live!

Get 1:1 Help Now