I have two tables
TableA has three fields
Id | FieldA | SomethingElse
TableB has three fields as well
Id | FieldA_FK | FieldB
We can join the tables on
TableA.FieldA = TableB.FieldA_FK
I would like to select values on both these tables in order to retrieve the following dataset:
TableA.Id, TableA.FieldA, TableA.SomethingElse, [Concatenation of TableB.FieldB]
To retrieve [Concatenation of TableB.FieldB], I know I can do
declare @result varchar(500);
set @result = '';
select @result = COALESCE(@result + ',', '') + FieldB
from TableB b
join TableA a on a.FieldA = b.FieldA_FK
select @result
How can I get the result described above with the concatenation on one result row only?
Thanks in advance.
Examples of data:
TableA
1 A something
2 B somethingElse
TableB
1 A Aa
2 A Ab
3 A Ac
4 B Ba
5 B Bb
I would like to retrieve
1 A something Aa, Ab, Ac
2 B somethingElse Ba, Bb