Solved

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

Posted on 2011-03-16
4
264 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya 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 41

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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

617 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