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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:40:22+00:00 2026-05-18T11:40:22+00:00

There are two tables, Table A A_ID A_Field 1 blah1 2 blah2 3 blah3

  • 0

There are two tables,

Table A

A_ID    A_Field  
1          blah1  
2          blah2  
3          blah3  
........

============================================================

Table B (A_ID is foreign key references Table A’s A_ID)

A_ID  B_Field  
1      a   
1      b  
1      c  
2      a  
2      b  
2      c  

What is the BEST way to get result like below:

A_ID   A_Field   B_Field  
1        blah1       a,b,c  
2        blah2       a,b,c

Thanks a lot for the replies, they all works, however, there is one more request, “For XML” dows NOT work on SQL SERVER 2000, unfortunately my domain service’s DB is SQL Server 2000, is there a simple query work on SQL SERVER 2000?? Thanks!

  • 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-18T11:40:22+00:00Added an answer on May 18, 2026 at 11:40 am

    Try something like (* Full Example* )

    DECLARE @TableA TABLE(
            A_ID INT,
            A_Field VARCHAR(20)
    )
    INSERT INTO @TableA SELECT 1,'blah1'
    INSERT INTO @TableA SELECT 2,'blah2'
    INSERT INTO @TableA SELECT 3,'blah3'
    
    DECLARE @TableB TABLE(
            A_ID INT,
            B_Field VARCHAR(20)
    )
    INSERT INTO @TableB SELECT 1,'a'
    INSERT INTO @TableB SELECT 1,'b'
    INSERT INTO @TableB SELECT 1,'c'
    INSERT INTO @TableB SELECT 2,'a'
    INSERT INTO @TableB SELECT 2,'b'
    INSERT INTO @TableB SELECT 2,'c'
    
    ;WITH Vals AS (
        SELECT  a.A_ID,
                a.A_Field,
                b.B_Field
        FROM    @TableA a INNER JOIN
                @TableB b ON a.A_ID = b.A_ID
    )
    SELECT  p1.A_ID,
            p1.A_Field
            ,STUFF( 
                   (SELECT 
                        ', ' + p2.B_Field 
                        FROM Vals p2 
                        WHERE p2.A_ID=p1.A_ID 
                        ORDER BY p2.A_ID 
                        FOR XML PATH(''), TYPE 
                   ).value('.','varchar(max)') 
                   ,1,2, '' 
              ) AS B_Field 
    FROM Vals p1 
    GROUP BY    p1.A_ID,
                p1.A_Field
    

    Output

    A_ID    A_Field B_Field
    1   blah1   a, b, c
    2   blah2   a, b, c
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

One doubt in MSSQL. There are two tables in a databases. Table 1 named
If I have two tables... Category and Pet. Is there a way in LINQ
I have two tables : create table CurrentDay ( ID int identity primary key,
I have two tables: CREATE TABLE InmarsatZenith.dbo.ClientJob (JobRef int PRIMARY KEY NOT NULL, IntRef
I have two tables that I'm querying from. In one table, there are fields
There are two tables, one is an Answer table and the other is a
I have two tables called messages and users . In the messages table, there's
We have two tables in a SQL Server 2005 database, A and B.There is
I am developing a student registration form in asp.net. there are two tables viz
I have a table in which there are two columns : 1. import type,

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.