multiple rows into one row

  • Hi,

    I have this table:

    ID        Value

    1          A        

    1          B        

    2          C        

    2          B        

    3          C        

    3          A        

    3          B        

    4          C        

    I would like to get the above table in the form:

    ID       Value

    1         A,B

    2         C,B

    3         C,A,B

    4         C

    Please let me know regarding this.

    Thanks.

     

  • You need to write a function that takes in the ID and returns a concatenated string of Values. create a function with something like this:

    declare @res varchar(100)

    select @res = isnull(@res,'' ) + ',' + value from yourtable where Id = @t

    return @res

    then call the function in the SELECT as

    SELECT ID, dbo.fnGetValues(Id)

    FROM yourTable

    ******************
    Dinakar Nethi
    Life is short. Enjoy it.
    ******************

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply