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 888395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:23:25+00:00 2026-05-15T13:23:25+00:00

Say I have the following table: id|myId|Name ————- 1 | 3 |Bob 2 |

  • 0

Say I have the following table:

id|myId|Name
-------------
1 | 3  |Bob 
2 | 3  |Chet
3 | 3  |Dave
4 | 4  |Jim
5 | 4  |Jose
-------------

Is it possible to use a recursive CTE to generate the following output:

3 | Bob, Chet, Date
4 | Jim, Jose

I’ve played around with it a bit but haven’t been able to get it working. Would I do better using a different technique?

  • 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-15T13:23:26+00:00Added an answer on May 15, 2026 at 1:23 pm

    I do not recommend this, but I managed to work it out.

    Table:

    CREATE TABLE [dbo].[names](
        [id] [int] NULL,
        [myId] [int] NULL,
        [name] [char](25) NULL
    ) ON [PRIMARY]
    

    Data:

    INSERT INTO names values (1,3,'Bob')
    INSERT INTO names values 2,3,'Chet')
    INSERT INTO names values 3,3,'Dave')
    INSERT INTO names values 4,4,'Jim')
    INSERT INTO names values 5,4,'Jose')
    INSERT INTO names values 6,5,'Nick')
    

    Query:

    WITH CTE (id, myId, Name, NameCount)
         AS (SELECT id,
                    myId,
                    Cast(Name AS VARCHAR(225)) Name,
                    1                          NameCount
             FROM   (SELECT Row_number() OVER (PARTITION BY myId ORDER BY myId) AS id,
                            myId,
                            Name
                     FROM   names) e
             WHERE  id = 1
             UNION ALL
             SELECT e1.id,
                    e1.myId,
                    Cast(Rtrim(CTE.Name) + ',' + e1.Name AS VARCHAR(225)) AS Name,
                    CTE.NameCount + 1                                     NameCount
             FROM   CTE
                    INNER JOIN (SELECT Row_number() OVER (PARTITION BY myId ORDER BY myId) AS id,
                                       myId,
                                       Name
                                FROM   names) e1
                      ON e1.id = CTE.id + 1
                         AND e1.myId = CTE.myId)
    SELECT myID,
           Name
    FROM   (SELECT myID,
                   Name,
                   (Row_number() OVER (PARTITION BY myId ORDER BY namecount DESC)) AS id
            FROM   CTE) AS p
    WHERE  id = 1 
    

    As requested, here is the XML method:

    SELECT myId,
           STUFF((SELECT ',' + rtrim(convert(char(50),Name))
            FROM   namestable b
            WHERE  a.myId = b.myId
            FOR XML PATH('')),1,1,'') Names
    FROM   namestable a
    GROUP BY myId
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say i have a table with the following data Customer table: Name amount
Let's say I have the following table: Employee name start_date end_date John 2009-10-10 2009-12-31
Say I have the following table: name ---- A B C D E F
Let's say I have the following table: CustomerID ParentID Name ========== ======== ==== 1
Let's say I have the following table, called GPAs : name | semester |
Let's say I have the following table : Name - Country - Age --------------------
I am wondering if the following is possible. Say I have the following table:
Say I have the following table called fruits : id | type | name
Let's say I have following table like this <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0
Let's say I have the following table: Value Time 0 15/06/2012 8:03:43 PM 1

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.