SQL Server: how to generate comma separated string for distinct values in aggregated columns in group by statement -


this question has answer here:

i generate comma separated string distinct occurrences aggregated column in group statement. have:

select column1, ???statement involving column2??? mytable group column1 

i can't figure out put between questions marks.

assuming have got following tables:

use tsql2012      if object_id('teststackoverflow') not null drop table teststackoverflow     create table teststackoverflow(     column1 varchar(100) not null,     column2 varchar(100) not null,     constraint pk2 primary key(column1,column2)      )      insert teststackoverflow(column1,column2)     values      ('value1','t1'),     ('value1','t2'),     ('value1','t3'),     ('value2','t5'),     ('value2','t6'),     ('value2','t7') 

you can use following query:

   select column1,      substring(         (             select ','+sto2.column2  [text()]             teststackoverflow sto2             sto2.column1 = sto1.column1             order sto2.column1             xml path ('')         ), 2, 1000) column2concat teststackoverflow sto1 group column1 

and result :

enter image description here

i using answer made ritesh reference: concatenate many rows single text string?


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -