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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:02:54+00:00 2026-06-18T08:02:54+00:00

I have a table eg assume this setup table MyTable has various columns Id

  • 0

I have a table eg assume this setup

table MyTable has various columns Id, UserId, col1, col2 col3 including column called Stuff.

I want to output certain Columns from MyTable with a query
but i want to split the ‘Stuff’ column such that 2 new columns are shown in the query

I can define the categories hardcoded, im not sure how this can be represented in sql

Categoy1 = "alpha, bravo, delta, gamma';
Categoy2 = "charlie, echo, hotel';



MyTable

ID |    UserID |      Stuff       | Other Cols....
----------------------------------------------------------
1          1          alpha
2          2          hotel
3          1          charlie
4          1          echo
5          1          gamma
6          2          bravo
7          2          delta

i want the select query to show

UserId  |  Category1           |  Catergory2
----------------------------------------------------------
1            alpha, gamma            charlie, echo
---------------------------------------------------------
2            bravo, delta            hotel
----------------------------------------------------------

i.e produce 2 columns split based on whether the stuff column contains an item from category1 or category2
based on a distinct userId the categories content can be comma separated as hown above

Please can you show how this can be done

Hope this makes sense.

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-06-18T08:02:55+00:00Added an answer on June 18, 2026 at 8:02 am

    You can use the xml extensions to concatenate your strings, then just hard code the categories into each subquery:

    CREATE TABLE #T (ID INT, UserID INT, [Stuff] VARCHAR(300))
    INSERT #T VALUES
        (1, 1, 'alpha'),
        (2, 2, 'hotel'),
        (3, 1, 'charlie'),
        (4, 1, 'echo'),
        (5, 1, 'gamma'),
        (6, 2, 'bravo'),
        (7, 2, 'delta');
    
    SELECT  UserID,
            [Category1] = STUFF((   SELECT  ', ' + [Stuff]
                                    FROM    #T t2
                                    WHERE   [Stuff] IN ('alpha', 'bravo', 'delta', 'gamma')
                                    AND     t.UserID = t2.UserID
                                    FOR XML PATH(''), TYPE
                                ).value('.', 'NVARCHAR(MAX)'), 1, 2, ''),
            [Category2] = STUFF((   SELECT  ', ' + [Stuff]
                                    FROM    #T t2
                                    WHERE   [Stuff] IN ('charlie', 'echo', 'hotel')
                                    AND     t.UserID = t2.UserID
                                    FOR XML PATH(''), TYPE
                                ).value('.', 'NVARCHAR(MAX)'), 1, 2, '')
    FROM    (   SELECT  DISTINCT UserID
                FROM    #T
            ) t
    

    Example on SQL Fiddle

    You could define your categories at the start in a CTE (Categories) for improved readibility:

    WITH Categories AS
    (   SELECT  Category, Name
        FROM    (VALUES
                    (1, 'alpha'), 
                    (1, 'bravo'), 
                    (1, 'delta'), 
                    (1, 'gamma'),
                    (2, 'charlie'),
                    (2, 'echo'), 
                    (2, 'hotel')
                ) t (Category, Name)
    ), Data AS
    (   SELECT  UserID, [Stuff], Category
        FROM    T
                INNER JOIN Categories c
                    ON c.Name = T.[Stuff]
    )
    SELECT  UserID,
            [Category1] = STUFF((   SELECT  ', ' + [Stuff]
                                    FROM    Data t2
                                    WHERE   Category = 1
                                    AND     t.UserID = t2.UserID
                                    FOR XML PATH(''), TYPE
                                ).value('.', 'NVARCHAR(MAX)'), 1, 2, ''),
            [Category2] = STUFF((   SELECT  ', ' + [Stuff]
                                    FROM    Data t2
                                    WHERE   Category = 2
                                    AND     t.UserID = t2.UserID
                                    FOR XML PATH(''), TYPE
                                ).value('.', 'NVARCHAR(MAX)'), 1, 2, '')
    FROM    (   SELECT  DISTINCT UserID
                FROM    T
            ) t
    

    Example on SQL Fiddle

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

Sidebar

Related Questions

Assume I have the following style table, col1 col2 and col3 have same value
Assume I have a table like this: column A(int) column B(int) 1 2 2
MY base table have two colums assume col1,col2. col1 have id, col2 have many
I have a city table that has two columns from_city to_city now lets assume
Assume I have a table called table and I have 3 columns, a, b,
Let's assume that I have mytable in mySQL CREATE TABLE `mytable` ( `gender` enum('MALE','FEMALE','UNISEX')
I have a data tables with this type of setup. Table A AID AName
Assume you have already done a defdb. I have a table items which has
Assume I have two tables: Table MY_ENTITY ID: PK OTHER_ID: FK to table OTHER
Suppose you have a standard org hierarchy table in Oracle. For simplicity sake, assume

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.