I have a query that returns a result set of one column, but I want it to be converted into one long string for use as a subquery. I.E. I want to be able to do:
SELECT user.*, (SELECT group_name FROM user_groups WHERE userID = user.ID) AS Groups FROM user
However, that will fail because a user can belong to more than one group. For example if the user belong to {"writer", "editor"} I want it to return a string like this: "writer, editor".
How can I do this?
You can use
FOR XMLto do this pivoting action. Here is a working sample.