-5

I have one table like this..

TicketID    Name   

111         ABC
111         DEF
222         GHI
333         ABC
333         GHI

I need output like this..

TicketID    Name   

111         ABC, DEF
222         GHI
333         ABC, GHI

I have found a same kind of query try to do it with Stuff function but don't know why m not getting exact result Can anyone give me query for SQL Server.. n tell me how actually it process.

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

1

Like here, here or here.

SELECT 
  [TicketId],
  STUFF((
    SELECT ', ' + [Name]) 
    FROM [OneTable] 
    WHERE ([TicketId] = OT.[TicketId]) 
    FOR XML PATH(''),TYPE).value('(./text())[1]','VARCHAR(MAX)')
  ,1,2,'') AS Name
FROM [OneTable] OT
GROUP BY [TicketId]

Go and vote it up, then close this question.

Community
  • 1
  • 1
Jodrell
  • 34,946
  • 5
  • 87
  • 124