0

I need a little help with a query I'm trying to run.

Basically I have two columns (trying to keep it simple) and I need to merge and concat These two columns are created from different tables with the following query:

select tmatter, tkinitb 
  FROM timecard, timekeep, matter
  where ttk = tkinit
  and mstatus = 'OP'
  group by tkinitb, tmatter
  order by tmatter ASC

tmatter  | tkinitb
1        |   A
1        |   B
2        |   C
2        |   D

I'm looking to have the results like this:

tmatter  | tkinitb
1        | A, B
2        | C, D

I'm not sure how to merge the different cells on the like cells.

Any help would be greatly appreciated.

T.J.
  • 733
  • 9
  • 27

2 Answers2

0
select [Column  1],
       stuff((select ',' + [Column 2] from yourtable x where x.[Column 1] = t.[Column 1] for xml path('')), 1, 1, '')
from   yourtable t
group by [Column  1]
Squirrel
  • 23,507
  • 4
  • 34
  • 32
0
SELECT  [COLUMN 1]
,STUFF( (SELECT  ',' + [COLUMN 2]
              FROM test T1
              WHERE T1.[column 1] = T2.[column 1]
              FOR XML PATH ('')
               ), 1, 1, '') 
FROM test T2
GROUP BY [column 1]
radar
  • 13,270
  • 2
  • 25
  • 33