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

  • Home
  • SEARCH
  • 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 6362991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:58:44+00:00 2026-05-24T23:58:44+00:00

I have MainTable with 2 fields: MAIN_ID and DESCRIPTION and SeoncdaryTable with 2 fields:

  • 0

I have MainTable with 2 fields: MAIN_ID and DESCRIPTION and SeoncdaryTable with 2 fields: MAIN_ID (FK to MainTable) and EXTRA_INFO.

I would like to have a query that returns one record per MainTable record (select * from MainTable) plus having another result that shows me in comma separated fashion all the records of SecondaryTable (of course matcihing the MAIN_ID).

Moreover I would like in the WHERE condition to be able to filter for SecondaryTable fields.

Example:

MainTable:

MAIN_ID, DESCRIPTION

1, One

2, Two

3, Three

4, Four

Secondary Table

MAIN_ID; EXTRA_INFO;

1; ALPHA

1; BETA

1; GAMMA

3; BETA

4; ALPHA

4; GAMMA

and I want to query all the records where secondary table contains GAMMA, the expected results are

1; One; ALPHA, BETA, GAMMA

4, Four; ALPHA, GAMMA

how to achieve that?

  • 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-24T23:58:45+00:00Added an answer on May 24, 2026 at 11:58 pm
    declare @MainTable table (MAIN_ID int identity, [DESCRIPTION] varchar(10));
    insert into @MainTable values('One'),('Two'),('Three'),('Four'),('Five');
    
    declare @SecondaryTable table (MAIN_ID int, EXTRA_INFO varchar(10));
    insert into @SecondaryTable values
    (1, 'ALPHA'), (1, 'BETA'), (1, 'GAMMA'), (3, 'BETA'), (4, 'ALPHA'), (4, 'GAMMA');
    
    select MT.MAIN_ID,
           MT.[DESCRIPTION],
           stuff((select ', '+ST.EXTRA_INFO
                  from @SecondaryTable as ST
                  where MT.MAIN_ID = ST.MAIN_ID
                  for xml path(''), type).value('.', 'varchar(max)'), 1, 2, '') as EXTRA_INFO
    from @MainTable as MT
    where MT.MAIN_ID in (select MAIN_ID      
                         from @SecondaryTable
                         where EXTRA_INFO = 'GAMMA')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Maketable query in an Access db that could use an Autonumber
I have a table which needs 2 fields. One will be a foreign key,
I have a table that has a bunch of fields. The fields can be
I have two tables with hierarchyid fields, one of which is a staging table
I have a performance issue regarding a quite simple query that run for more
I have var row=<tr><td>val</td><td>val2</td></tr> and I tried this: $(#mainTable tbody).append(row); but it appends to
I have main table called 'Employee' and another slave table called 'EmployeeTypes' that has
I have a main table that I must get data from. I have a
I would like to export part of a database full of data to an
I have a pretty big web application that I created last year using ASP.NET

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.