Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 884825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:48:26+00:00 2026-05-15T12:48:26+00:00

I have a query that returns a result set of one column, but I

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T12:48:27+00:00Added an answer on May 15, 2026 at 12:48 pm

    You can use FOR XML to do this pivoting action. Here is a working sample.

    set nocount on
    
    declare @Users Table
    (
        UserId  int,
        UserName varchar (20)
    )
    
    declare @UserGroups Table
    (
    GroupId int,
    UserId int,
    GroupName varchar (20)
    )
    
    Insert @Users Values (1, 'Tim')
    Insert @Users Values (2, 'Jane')
    Insert @Users Values (3, 'Hal')
    
    Insert @UserGroups Values (1, 1, 'Admin')
    Insert @UserGroups Values (2, 1, 'Power-users')
    Insert @UserGroups Values (3, 2, 'Noobs')
    Insert @UserGroups Values (4, 2, 'Users')
    Insert @UserGroups Values (5, 3, 'Noobs')
    
    /* How this works */
    
    SELECT 'FirstStep  : Users table'
    SELECT * FROM @Users
    
    SELECT 'NextStep : User Groups table'
    SELECT * FROM @UserGroups
    
    SELECT 'NextStep : Users & UserGroups table'
    SELECT * 
    FROM @Users U
        INNER JOIN @UserGroups UG ON U.UserId = UG.UserId
    
    SELECT 'NextStep : Just get the groups for one user (UserId = 2)'
    SELECT GroupName
    FROM @UserGroups UG
    WHERE UG.userID = 2
    ORDER BY GroupName
    
    SELECT 'NextStep : When you use XML Path the output comes out in XML format'
    SELECT GroupName
    FROM @UserGroups UG
    WHERE UG.userID = 2
    ORDER BY GroupName
    FOR XML PATH('') -- XML Path added
    
    SELECT 'NextStep : When you remove the column name the XML tags go away, 
    but it looks ugly because there is no separator' 
    SELECT GroupName + '' -- Added an empty string to remove the column name
    FROM @UserGroups UG
    WHERE UG.userID = 2
    ORDER BY GroupName
    FOR XML PATH('') 
    
    SELECT 'NextStep : Add a separator
    We add it to the beginning instead of the end so that we can STUFF later on.' 
    SELECT ', ' + GroupName -- Added an empty string to remove the column name
    FROM @UserGroups UG
    WHERE UG.userID = 2
    ORDER BY GroupName
    FOR XML PATH('') 
    
    SELECT 'NextStep : I don''t like that ugly XML column name. Let me give it my own name' 
    SELECT 
    (
        SELECT ', ' + GroupName
        FROM @UserGroups UG
        WHERE UG.userID = 2
        ORDER BY GroupName
        FOR XML PATH('') 
    ) as UserGroups
    
    SELECT 'NextStep : STUFF the extra comma' 
    SELECT STUFF 
        (
            (
                SELECT ', ' + GroupName
                FROM @UserGroups UG
                WHERE UG.userID = 2
                ORDER BY GroupName
                FOR XML PATH('') 
            ),
            1, 2, ''
        ) as UserGroups
    
    
    SELECT 'NextStep : Now join it with the Users table by the UserId and get the UserName' 
    SELECT 
        U.UserName, 
        STUFF 
        (
            (
                SELECT ', ' + GroupName
                FROM @UserGroups UG
                WHERE UG.userID = U.userID
                ORDER BY GroupName
                FOR XML PATH('') 
            ),
            1, 2, ''
        ) as UserGroups
    FROM @Users U
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query that returns a result set similar to the one below:
I have a query that returns a result set similar to the one below
I have a query that returns one row. However, I want to invert the
I have a very complex Linq to SQL query that returns a result set
I have a query that returns a string, as well as an escape character
i have a query that returns rows that i want, e.g. QuestionID QuestionTitle UpVotes
I have a query that returns a lot of data into a CSV file.
I have a query which returns a result set which looks like this: A
I currently have a query that returns a list of columns, and one of
I have this query that returns the results by ordering it by focus.name ASC.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.