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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:56:42+00:00 2026-05-23T17:56:42+00:00

I have three tables as follows: MasterTable +———-+————-+ | MasterId | MasterName | +———-+————-+

  • 0

I have three tables as follows:

MasterTable

+----------+-------------+
| MasterId | MasterName  |
+----------+-------------+
| 1        | Master 1    |
| 2        | Master 2    |
| 3        | Master 3    |
| 4        | Master 4    |
+----------+-------------+

ChildrenTable

+----------+-------------+
| ChildId | ChildName    |
+----------+-------------+
| 1        | Child 1     |
| 2        | Child 2     |
| 3        | Child 3     |
| 4        | Child 4     |
+----------+-------------+

LinkTable

+----------+-----------------------+
| Id       | MasterId    | ChldId  |
+----------+-----------------------+
| 1        |  1          | 1       | 
| 2        |  2          | 1       | 
| 3        |  3          | 2       | 
| 4        |  4          | 3       | 
+----------+-----------------------+

One child can be linked with multiple masters and LinkTable contains this detail. I want a query to select the following:

1, 'Child 1', 'Master 1, Master 2', '1,2'
2, 'Child 2', 'Master 2', '2'
3, 'Child 3', 'Master 3', '3'

Is it possible to do without loops or calling additional function, using COALESCE, STUFF, recursive CTE etc?

  • 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-23T17:56:42+00:00Added an answer on May 23, 2026 at 5:56 pm

    To concatenate strings you can use this method: How to concatenate all strings from a certain column for each group

    Test data:

    declare @masterTable table(MasterId int identity, MasterName varchar(max))
    insert @masterTable (MasterName) values('m1'), ('m2'), ('m3'), ('m4')
    
    declare @childrenTable table(ChildId int identity, ChildName varchar(max))
    insert @childrenTable (ChildName) values('c1'), ('c2'), ('c3'), ('c4')
    
    declare @LinkTable table(MasterId1 int, MasterId2 int, ChildId int)
    insert @LinkTable values(1,1,1), (2,2,1), (3,3,2), (4,4,3)
    

    Query:

    select t.*
    from
    (
        select c.ChildId, c.ChildName
    
            , STUFF((
                select ', ' + m.MasterName
                from
                (
                    select l.MasterId1
                    from @LinkTable l
                    where l.ChildId = c.ChildId
    
                    union
    
                    select l.MasterId2
                    from @LinkTable l
                    where l.ChildId = c.ChildId
                )t
                join @masterTable m on m.MasterId = t.MasterId1
                for xml path(''), type
            ).value('.', 'varchar(max)'), 1, 2, '') [names]
    
            , STUFF((
                select ', ' + cast(t.MasterId1 as varchar(max))
                from
                (
                    select l.MasterId1
                    from @LinkTable l
                    where l.ChildId = c.ChildId
    
                    union
    
                    select l.MasterId2
                    from @LinkTable l
                    where l.ChildId = c.ChildId
                )t
                for xml path(''), type
            ).value('.', 'varchar(max)'), 1, 2, '') [ids]
        from @childrenTable c
    )t
    where t.ids is not null
    

    Output:

    ----------- --- -------- ------
    1           c1  m1, m2   1, 2
    2           c2  m3       3
    3           c3  m4       4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, I'm going to say that I have three tables as follows: POSTS POSTS_TAGS
I have three tables in MSSQL for a game which are structured as follows:
alright, so lets say i have three tables, as follows : employees( ID ,
I have three tables setup: POSTS, DISCUSSIONS, and COMMENTS. The layout is as follows:
I have three tables like follows: products id | name 1 | name1 2
I have three tables: page, attachment, page-attachment I have data like this: page ID
I have three tables tag , page , pagetag With the data below page
I have three tables in the many-to-many format. I.e, table A, B, and AB
I have three tables like that: Articles IdArticle Title Content Tags IdTag TagName ContentTag
I have three tables. This query will write down the right answer (x-lines for

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.