0

I'm trying to concatenate multiple rows of data into one row based off of another column's ordered value

Here's the data that I'm working with:

Here's the data picture

Here's the desired output:

37960 BRAKES GENERAL REPAIRS ADDITIONAL REPAIRS

I'm working in SSMS V14.0 Thanks!

JBrewer
  • 13
  • 4
  • So how do you know that these values belong together? What is the common identifier? – Igor Aug 26 '19 at 15:07
  • Don't post images of textual data. Images are useful for graphics, things that can't be communicated as text. – Igor Aug 26 '19 at 15:09
  • Updated with an image with an identifier column of data. Thanks – JBrewer Aug 26 '19 at 15:09
  • Hint: If you are using SQL-SERVER 2016 or before then `FOR XML PATH`. If using SQL-SERVER 2017 or later then `STRING_AGG`. – Ankit Bajpai Aug 26 '19 at 15:10
  • That can't be all the data you are working with. Where is the rest? – daShier Aug 26 '19 at 15:10
  • 1
    Do you have any column, through which you can group every set of 0, 1 and 2? – mkRabbani Aug 26 '19 at 15:12
  • No, the order of the concatenation follows the ascending number values in the Segment column – JBrewer Aug 26 '19 at 15:15
  • like mkRabbani is saying. There needs to be something linking the comments together other than the segment column. How would you determine that the next segments, 3 and 4, belong or don't belong? Is the result set you posted coming from a table? If so can you post the schema? – BillRuhl Aug 26 '19 at 15:19
  • Cannot figure out how to post the correct question. There is another column marked service order that has all of the segments and then the following notes if that helps answer your question. – JBrewer Aug 26 '19 at 15:29
  • Had to revert to using a picture as I can't figure out how to post the question correctly. apologies to all and thanks for the patience – JBrewer Aug 26 '19 at 15:57
  • There are two links at the top of your question. They will lead you to the answer you're looking for. All you have to do is add an `order by` to the query. – Zohar Peled Aug 26 '19 at 16:25

0 Answers0