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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:15:40+00:00 2026-06-16T22:15:40+00:00

I have some tables in SQL Server 2005: Product : ID, Name Category :

  • 0

I have some tables in SQL Server 2005:

  • Product: ID, Name
  • Category: ID, Name
  • Tags: ID, tagName
  • ProductCategory: productId, tagId
  • CategoryTags: categoryId, tagId

Basically I need a query that will display the category and list of tags associated for each product on ONE line.

For example, there are 3 categories: Animals, Countries, Color.

Each one of those have a bunch of tags.

Lets say that Product 1 has a bunch of tags associated such as: bird, duck, dog, canada, russia, japan, black, red, white

I need the query result to be in the format:

productId, [Category:tag,tag,tag;Category:tag,tag,tag:Category:tag,tag,tag]

1, [Animal:bird,duck,dog;Country:canada,russia,japan;Color:black,red,white]

The text in the square brackets should be in one column returned by SQL.

I found something similar here: Concatenate many rows into a single text string?

But I need to take it a step further and have it all on one line instead of it returning the different categories on separate rows.

Kinda hard to explain but hope you get it.

Is this possible?

Thanks in advance.

UPDATE: Thanks for everyone’s help and input. Really appreciate it!
Here is what I have so far which is close, but not quite there yet. Perhaps it’ll help you figure it out for me 😀

http://sqlfiddle.com/#!3/eed14/5

  • 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-16T22:15:42+00:00Added an answer on June 16, 2026 at 10:15 pm

    This is a really ugly way to get this data into a single string. But you can use something like this:

    select distinct 
      p.name ProductName,
      STUFF((SELECT distinct ', ' + 
               c.name +':'+ STUFF((SELECT DISTINCT ', ' + Name 
                                   FROM tags t
                                   LEFT JOIN CategoryTags ct
                                      on t.id = ct.tagid
                                   WHERE c.id = ct.categoryid
                                   FOR XML PATH('')), 1, 1, '')
             FROM category c 
             LEFT JOIN CategoryTags ct
               ON ct.categoryid = c.id
             LEFT JOIN productcategory pc
               ON pc.tagid = ct.tagid
             WHERE p.id = pc.productid
             FOR XML PATH('')), 1, 1, '')  List
    from product p
    

    See SQL Fiddle with Demo

    The result is:

    | PRODUCTNAME |                                                                               LIST |
    ----------------------------------------------------------------------------------------------------
    |        Test |  Animal: Bird, dog, duck, Color: black, red, white, Country: canada, japan, russia |
    

    Edit #1: This is producing the result that you want:

    select distinct 
      p.name ProductName,
      STUFF((SELECT distinct '; ' + 
               c.name +':'+ STUFF((SELECT DISTINCT ', ' + Name 
                                   FROM catalogTags t
                                   LEFT JOIN catalogProductTags pt
                                     on t.id = pt.catalogTagId
                                   LEFT JOIN catalogCategoryTags ct
                                     on pt.catalogTagId = ct.catalogTagId
                                   WHERE c.id = ct.catalogCategoryId
                                      AND p.id = pt.catalogProductId
                                   FOR XML PATH('')), 1, 1, '')
             FROM catalogProductTags pt
             LEFT JOIN catalogCategoryTags ct
               ON pt.catalogTagId = ct.catalogTagId
             LEFT JOIN catalogCategory c 
               ON ct.catalogCategoryId = c.id
             WHERE p.id = pt.catalogProductId
             FOR XML PATH('')), 1, 1, '')  List
    from catalogProduct p;
    

    See SQL Fiddle with Demo. This can probably be refactored to a cleaner version.

    The result is:

    | PRODUCTNAME |                                                        LIST |
    -----------------------------------------------------------------------------
    |     tshirt1 |                           Animals: dog; Color: black, white |
    |     tshirt2 |              Animals: dog; Color: blue, red; Countries: USA |
    |     tshirt3 |  Color: blue, pink, red, white; Countries: Australia, Japan |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using SQL Server 2005. I have a series of product and pricing tables
I'm running SQL Server 2005 and .Net 2.0. I have some tables that need
I have a Sql Server 2005 Standard production database with some <120 tables and
I have a SQL Server 2005 database in which I have some tables contain
I'm using SQL Server (2005). Today I have some tables that everytime the data
I have a SQL server and couple Windows clients and cache of some tables
i have made columns in some of the tables encrypted in sql server 2008.
I came around some weird thing in MS SQL server. I have two tables
I have two tables (MS SQL Server 2005) with an existing application (no DB
I am using SQL server 2005. In one of the tables, I have a

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.