вторник, 31 января 2017 г.

Конкатенация строк с группировкой


select 
 id
 ,stuff
 (
  (
   select
    ' and ' + name
   from
    table_name
   where
    (id = results.id)
    
   for xml path(''),type

    /* Use .value to uncomment XML entities e.g. > < etc*/
  ).value('.','varchar(max)')
  ,1 ,5 ,''
 ) as join_rule

from
 table_name results

group by
 id