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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:45:57+00:00 2026-06-09T12:45:57+00:00

Possible Duplicate: SQL Server: Can I Comma Delimit Multiple Rows Into One Column? I

  • 0

Possible Duplicate:
SQL Server: Can I Comma Delimit Multiple Rows Into One Column?

I would like to combine all records in a certain field into a single cell (per value from another column) if the count of the records is more than 1. For example, if I have the following code

SELECT city, count(zoo name) AS 'count of zoo name' FROM mytable

It would generate the below results

enter image description here

The original table looks like this

enter image description here

Since both Atlanta and New York have more than one zoo and Tokyo only has one zoo, the final result should look like

enter image description here

How would I go about doing this? I thought about using the PIVOT construct, but that creates new columns for each possible value. I would also have to write the name of every possible zoo name into the PIVOT. This would be bad form since the actual data has much more possible values of “zoo name” than the above.

  • 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-06-09T12:45:59+00:00Added an answer on June 9, 2026 at 12:45 pm

    You can use XML Path to concat the column value

    ;With Cte(city,concat) as
    (
        select city, (select a.Zoo+','
        from Sample a
        where a.city=b.city
        for XML PATH ('') )  concat
        from Sample b
        group by city
     )
    Select city,left(concat, len(concat) -1) from cte
    

    Check the result in SQL Fiddle

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

Sidebar

Related Questions

Possible Duplicate: SQL Server: Can I Comma Delimit Multiple Rows Into One Column? I
Possible Duplicate: SQL Server: Can I Comma Delimit Multiple Rows Into One Column? I
Possible Duplicate: SQL Server: Can I Comma Delimit Multiple Rows Into One Column? Script
Possible Duplicate: SQL Server: Can I Comma Delimit Multiple Rows Into One Column? I
Possible Duplicate: Selecting data from two different servers in SQL Server How can I
Possible Duplicate: How can I determine installed SQL Server instances and their versions? i've
Possible Duplicate: Can a number used to name a sql column I am trying
Possible Duplicate: SQL Server UDF refresh The system stored procedure sp_refreshview can be used
Possible Duplicate: How does SQL server work out the estimated number of rows? I
Possible Duplicate: How to update two tables in one statement in SQL Server 2005?

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.