SQL Server 2012 Columns to Single Row Seperated by Comma -


i have table sortorder , diagnosiscodes.

enter image description here

i need see diagnosiscodes in 1 row each different set of sortorders.

enter image description here

enter image description here

use xml path

declare @diagnosis table (id int, diagnosiscode varchar(10)) insert @diagnosis values (1,'d50.9'), (1,'m10.9'), (1,'z79.82'), (2,'m81.0'), (2,'z85.3'), (2,'z90.710'), (3,'m81.0'), (3,'z85.3'), (3,'z17.0')   select t.id,        stuff(( select ', ' + diagnosiscode                 @diagnosis                 id = t.id                 xml path(''),type)                 .value('.','nvarchar(max)'),1,2,'') alldiagnosiscodes   @diagnosis t  group t.id  order t.id 

result

id  alldiagnosiscodes 1   d50.9, m10.9, z79.82 2   m81.0, z85.3, z90.710 3   m81.0, z85.3, z17.0 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -