-3

I need to fetch the data from SQL Server based on the below condition.

In the database there are columns Ticketnumber and comments. For a Ticketnumber there are multiple records, for the same ticket number there are multiple comments stored in different records. Whenever I retrieve the ticketnumber and comments columns, the comments should be concatenated in the single record.

Let's look at an example:

Ticketnumber       Comments        
-------------------------------
1002               Case raised    
1003               Case raised
1002               Processing,
1003               resolved
1002               resolved

Expected output should be:

Ticketnumber       Comments
-----------------------------------------------
1002               Case raise,processing,resolved,
1003               case raised,resolved  

All the comments should be concatenated for single ticketnumber as above.

Please provide the query. Thanks in advance

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
shaik ahamad
  • 63
  • 1
  • 9

2 Answers2

0

There was a previous post w/ an answer for something similar here:

Concatenate many rows into a single text string?

Community
  • 1
  • 1
Graham
  • 336
  • 1
  • 13
  • you should post the answer here as well (along with the link with the source), since the link may become unavailable. – cr0ss Mar 31 '15 at 19:56
  • It's a stackoverflow link. If anything, he should flag the question as a duplicate. – Tab Alleman Mar 31 '15 at 20:07
0

Try this:

Select X.Ticketnumber,
       Left(X.yourtable,Len(X.yourtable)-1) As "comments"
From
    (
        Select distinct y.Ticketnumber, 
            (
                Select x.comments  + ',' AS [text()]
                From dbo.yourtable x
                Where x.Ticketnumber= y.Ticketnumber
                ORDER BY x.SubjectID
                For XML PATH ('')
            ) [yourtable]
        From dbo.yourtable y
    ) [X]

Taken help from the source thread

Community
  • 1
  • 1
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331