I have two tables as follows:
Users
| Name | TagsId |
+--------+--------+
| Bob | 3 |
| Jim | 2 |
| Bob | 2 |
| Frank | 3 |
| Jim | 1 |
Tags
| Name | Id |
+---------+-----+
| web | 1 |
| desktop | 2 |
| phone | 3 |
My desired result is as follows:
| Name | TagList |
+-------+----------------+
| Bob | desktop, phone |
| Jim | desktop, web |
| Frank | phone |
I am able to accomplish this in SQL Server 2017 using the following query:
SELECT
u.name, STRING_AGG (t.name, ', ') as taglist
FROM
users AS u
LEFT JOIN
Tags AS t ON u.TagsId = t.Id
GROUP BY
u.name;
But unfortunately my data is in a SQL Server 2016 server so cannot use the STRING_AGG
function.
How can I accomplish this without STRING_AGG
?