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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:45:51+00:00 2026-05-30T17:45:51+00:00

I have a query that searches through several tables and returns one row for

  • 0

I have a query that searches through several tables and returns one row for every value in one specific column of one of the queried tables. The table returns multiple rows for one unique identifier. What I want to do is combine those rows that have the same unique identifier and combine 2 of the column’s value separated by commas and return those values as a unique column.

Example:

 Museum     MuseumID     Country     City     Paintings     Sculptures

 Louvre     345          France      Paris    Mona Lisa     NULL
 Louvre     345          France      Paris    NULL          Venus De Milo
 Louvre     345          France      Paris    Ship of Fools NULL

Instead I would like to make the query do this:

 Museum     MuseumID     Country     City     Art
 Louvre     345          France      Paris    Mona Lisa, Venus De Milo, Ship of Fools

I need to turn this query into a stored procedure that can be used in a C# program. At first I just took the data as is and used C# to combine the rows using arrays and some logic but I HAVE TO make this a stored procedure instead to make the data come over to the C# program already sorted and combined. I don’t want to I have to. I need help.

Can anyone help with 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-30T17:45:52+00:00Added an answer on May 30, 2026 at 5:45 pm

    Given this sample data:

    CREATE TABLE #a
    (
        Museum     varchar(32),
        MuseumID   int, 
        Country    varchar(32),
        City       varchar(32),
        Paintings  varchar(32),
        Sculptures varchar(32)
    );
    
    INSERT #a VALUES
    ('Louvre',345,'France','Paris', 'Mona Lisa',     NULL),
    ('Louvre',345,'France','Paris', NULL,            'Venus De Milo'),
    ('Louvre',345,'France','Paris', 'Ship of Fools', NULL);
    

    In older versions of SQL Server, we’d have to perform grouped string aggregation using a derived table that munged strings together using FOR XML PATH:

    SELECT Museum, MuseumID, Country, City, 
        Art = STUFF((SELECT ', ' + COALESCE(Paintings, Sculptures, '')
        FROM #a AS a2
        WHERE a2.museum = a.museum AND a2.MuseumID = a.MuseumID
        AND a2.Country = a.Country AND a2.City = a.City
        FOR XML PATH(''), 
        TYPE).value(N'./text()[1]', N'varchar(max)'), 1,2,'')
    FROM #a AS a
    GROUP BY Museum, MuseumID, Country, City;
    

    In SQL Server 2017+, we can use STRING_AGG() for a much simpler and more efficient query:

    SELECT Museum, MuseumID, Country, City,
      Art = STRING_AGG(COALESCE(Paintings, Sculptures, ''), ', ')
    FROM #a
    GROUP BY Museum, MuseumID, Country, City;
    

    Results in both cases:

    Museum MuseumID Country City Art
    Louvre 345 France Paris Mona Lisa, Venus De Milo, Ship of Fools
    • Example db<>fiddle

    (Note that if a painting or sculpture name has a comma in it, which is probably not rare, you won’t be able to tell. So sometimes it is better to choose a delimiter that has a really low probability of being in the data, like a pipe ('|') or use nvarchar as the base type and some Unicode character like double pipe (N'‖').)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a mysql/php query which searches through a database and returns matches based
I have a procedure that searches through the database and returns the results based
I have a query that searches for clients using like with wildcard. For example:
I have a query in my controller which searches for the term/keyword that is
I have this query that searches my database for accommodation that has a type
I am using in C# MYsql .I have query that works if I run
I have a query that is dynamically built after looking up a field list
I have a query that originally looks like this: select c.Id, c.Name, c.CountryCode, c.CustomerNumber,
I have a query that looks like this: public IList<Post> FetchLatestOrders(int pageIndex, int recordCount)
I have a query that I'm executing from a .NET application to a SQL

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.