0

I have about 10,000 ids where I need to merge notes column separated by ' | ' or ' , '

my data looks like this:

ID notes


   ID     Notes   Date
     1 notes1     3/1/20
     1 notes2     3/2/20
     1 notes3     3/3/20
     2 notes1
     2 notes2
     2 notes3
     3 notes1
     3 notes2

Desired output


   ID Column      NOTES
     1         3/1/20 notes1, 3/2/20 notes2, 3/3/20 notes3
     2         notes1,notes2,notes3
     3         notes1,notes2

how to achieve this in sql server (SSMS)

gauravk
  • 9
  • 3
  • 1
    Images of data really don't help us help you. Take the time to post tabular formatted `text` or, even better, as DDL and DML statements. Don't forget, as well, to **include** your attempts; that could be the code you wrote and the errors you got or it might be links to articles/documents you read and didn't understand. if the latter, tell us what you didn't understand. Also, what is your question? You haven't asked one here. – Thom A Mar 11 '20 at 14:14
  • For SQL Server, the duplicates should be all you need. If you have failed to implement these solutions post a new question, ensuring you follow my points in the prior comment. – Thom A Mar 11 '20 at 14:16
  • @Larnu Please look at the data now. I just updated my question – gauravk Mar 11 '20 at 14:18
  • That is an awesome edit! You'll want that `STUFF(... for xml path...)` version of [the answer on the listed duplicate](https://stackoverflow.com/a/15477847/2221001) for your SQL Server 2012. – JNevill Mar 11 '20 at 14:19
  • @JNevill I don't have any XML path for the file, how do I enter that – gauravk Mar 11 '20 at 14:22
  • It's just a hack. You don't have to have anything related to XML or even know what XML is. Just plug your columns/table into that same exact code as-is and run it and it will do exactly what you want. There's a couple of other RDBMS products (like Teradata) where we have to use a similar XML-based hack like this to perform a simple string_agg/list_agg operation that other databases support out of the box. It's ugly to look at, but it works. :) – JNevill Mar 11 '20 at 14:23
  • Ok I will try now. I tried using String_AGG but it throws error 'STRING_AGG' is not a recognized built-in function name. Thank you – gauravk Mar 11 '20 at 14:24
  • That's because `STRING_AGG` was added in SQL Server 2017, @gauravk . – Thom A Mar 11 '20 at 14:30
  • @JNevill - It worked, now I have another query related to the same case. there is another column called "notesDate" which also need to be concatenated with "notes" ID notesdate1,note1&notesdate2,note2 – gauravk Mar 11 '20 at 14:57
  • Just updated my question again to show what I am looking for – gauravk Mar 11 '20 at 15:02
  • Please ask a new question for that one. – JNevill Mar 11 '20 at 15:07

0 Answers0