lukaabra
lukaabra

Reputation: 83

Group several rows of data into one row by column?

I have a result of a query that looks like this:

enter image description here

Is it possible to write a query that will take the relationshipdescriptions from the above query and group them by person? For example:

  1. GUID1 Son, Spouse
  2. GUID2 Son
  3. GUID3 Aunt, Mother

I will be using this query in a CTE so I will not be able to use variables.

SQL Server 15.0.2

Upvotes: 0

Views: 27

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269553

You can use string_agg():

select personguid, string_agg(relationshipdescription, ',')
from t
group by personguid;

Upvotes: 2

Related Questions