String_Agg combines the values from a group one or more rows into a delimited list. For example, suppose you have a table like
Declare @Sample Table(CustomerID int, OrderID int)
Insert @Sample(CustomerID, OrderID) Values
(100, 75),
(210, 37),
(100, 47),
(330, 235),
(100, 255),
(210, 14);
Then you could get a comma delimited list of of each customer's orders by using String_Agg with
Select CustomerID, String_Agg(OrderID, ', ') As Orders
From @Sample
Group By CustomerID
Order By CustomerID;
The result would look like
100 75, 47, 255
210 14, 37
330 235
That gives a list of each customer's orders. But the list is in no particular order. If you the list to be in a given order, you would use Within Group. For example
Select CustomerID, String_Agg(OrderID, ', ') Within Group(Order By OrderID) As Orders
From @Sample
Group By CustomerID
Order By CustomerID;
Then the result would be
100 47, 75, 255
210 14, 37
330 235
Tom