sql - Can I Comma Delimit Multiple Rows Into One Column? -
this question has answer here:
- concatenate many rows single text string? 38 answers
 
i attempting merge in sql server database:
[ticketid], [person] t0001 alice t0001 bob t0002 catherine t0002 doug t0003 elaine
into this:
[ticketid], [people] t0001 alice, bob t0002 catherine, doug t0003 elaine
i need in both sql server , oracle.
i have found function group_concat mysql need here, mysql not option here.
edit: test bench:
declare @tickets table (     [ticketid] char(5) not null,     [person] nvarchar(15) not null )  insert @tickets values     ('t0001', 'alice'),     ('t0001', 'bob'),     ('t0002', 'catherine'),     ('t0002', 'doug'),     ('t0003', 'elaine')  select * @tickets      
here solution works in sql server 2005+:
select t.ticketid,        stuff(isnull((select ', ' + x.person                 @tickets x                x.ticketid = t.ticketid             group x.person              xml path (''), type).value('.','varchar(max)'), ''), 1, 2, '') [no preceeding comma],        isnull((select ', ' + x.person                 @tickets x                x.ticketid = t.ticketid             group x.person              xml path (''), type).value('.','varchar(max)'), '') [preceeding comma if not empty]   @tickets t group t.ticketid   reference:
Comments
Post a Comment