1

I have table:

NAME    FACTOR
say_type1    A
(...)
say_type2    B
(...)
say_type3    A
(...)
say_type3    B

I would like to get as result of an answer (SELECT name, factor FROM... WHERE some_condition):

say_type1    A
say_type2    B
say_type3    A,B

Im trying CASE but it acts always only for one record.

mpruchni
  • 290
  • 5
  • 17
  • 1
    Google: "SQL Server aggregate string concat" – Gordon Linoff Jul 22 '16 at 12:32
  • 1
    Possible duplicate of [Simulating group\_concat MySQL function in SQL Server?](http://stackoverflow.com/questions/451415/simulating-group-concat-mysql-function-in-sql-server) –  Jul 22 '16 at 12:35

1 Answers1

0

I guess you want to convert FACTOR column into row. You can make it with XML PATH:

--F.e. we have this in YourTable
--('say_type1', 'A'),
--('say_type2', 'B'),
--('say_type3', 'A'),
--('say_type3', 'B')

SELECT DISTINCT t1.NAME,
        STUFF((SELECT ',' + FACTOR
        FROM YourTable
        WHERE NAME = t1.NAME
        FOR XML PATH('')),1,1,'') as FACTOR
FROM YourTable t1

Output:

NAME        FACTOR
say_type1   A
say_type2   B
say_type3   A,B
gofr1
  • 15,741
  • 11
  • 42
  • 52