Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2010

    Unanswered: Combine Data from Multiple Rows Across All Rows

    Here's a simple example of what I want to do:

    Let's say I have a table "Feelings" with the following data in it:

    Id NameId Name Feeling
    1 1 Betty Happy
    2 1 Betty Sad
    3 1 Betty Mad
    4 2 Rex Mad

    I want to write a stored procedure that returns a table of results looking like this:

    Id NameId Name Feeling AllFeelings
    1 1 Betty Happy Happy, Sad, Mad
    2 1 Betty Sad Happy, Sad, Mad
    3 1 Betty Mad Happy, Sad, Mad
    4 2 Rex Mad Mad

    In other words, I need to keep the actual data of each individual record (row), but then add a column that references all values of feelings for each NameId. Thank you for any assistance! My apologies for not being able to figure out how to make a table properly in the editor
    Last edited by blasterv; 07-07-10 at 09:40.

  2. #2
    Join Date
    Dec 2007
    Richmond, VA
    Provided Answers: 5
    take a look at recursion/pivot/cte. This has been discussed many times in the past here on forum.

  3. #3
    Join Date
    Jun 2003
    Provided Answers: 1
    Are you on SQL Server 2005 or higher? This can be accomplished using a recursive CTE (Common Table Expression).
    If it's not practically useful, then it's practically useless.

    blindman "sqlblindman"

  4. #4
    Join Date
    Jul 2010


    Thanks. I figured it out.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts