This post is not intended to start a discussion about the best method of group concatenation of strings in SQL Server because many articles have already done that. I'd like to use a method like this for the group concatenation piece itself: https://groupconcat.codeplex.com/ but I'm open to other options. The issue is this: I need to see the history of the concats. Let's say I have data like this and can add more IDs as necessary:
╔═════════╦═════════════╦═══════════╦════════╦══════════════════════════════════════════════════════════════════════════════╗
║ chat_id ║ SpeakerName ║ SpeakerID ║ ConvID ║ Text ║
╠═════════╬═════════════╬═══════════╬════════╬══════════════════════════════════════════════════════════════════════════════╣
║ 1 ║ Ruby ║ 1 ║ 1 ║ I need help ║
║ 2 ║ Ms. Kary ║ 2 ║ 1 ║ Okay ║
║ 3 ║ Ruby ║ 1 ║ 1 ║ i do not get this problem i am confusd ║
║ 4 ║ Ms. Kary ║ 2 ║ 1 ║ In each of the possibie equations, see if it is the same as 9 ( x + 2) = 90. ║
║ 5 ║ Beth ║ 4 ║ 2 ║ where's ms Q ║
║ 6 ║ Ms J ║ 3 ║ 2 ║ Not here today. will you please work with me ? ║
║ 7 ║ Beth ║ 4 ║ 2 ║ kk thats fine ║
║ 8 ║ Ms J ║ 3 ║ 2 ║ what is 8 ÷ 10 written as a fraction ║
╚═════════╩═════════════╩═══════════╩════════╩══════════════════════════════════════════════════════════════════════════════╝
Using the group concat functions would result in the data looking like this (note that I'm using " @@@ " as a delimiter to make it more easily readable):
╔════════╦══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗
║ ConvID ║ ConcatenatedText ║
╠════════╬══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╣
║ 1 ║ In each of the possibie equations, see if it is the same as 9 ( x + 2) = 90. @@@ i do not get this problem i am confusd @@@ Okay @@@ I need help ║
║ 2 ║ what is 8 ÷ 10 written as a fraction @@@ kk thats fine @@@ Not here today. will you please work with me ? @@@ where's ms Q ║
╚════════╩══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝
What I need is this:
╔═════════╦═════════════╦═══════════╦════════╦══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗
║ chat_id ║ SpeakerName ║ SpeakerID ║ ConvID ║ ConcatenatedText ║
╠═════════╬═════════════╬═══════════╬════════╬══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╣
║ 1 ║ Ruby ║ 1 ║ 1 ║ I need help ║
║ 2 ║ Ms. Kary ║ 2 ║ 1 ║ Okay @@@ I need help ║
║ 3 ║ Ruby ║ 1 ║ 1 ║ i do not get this problem i am confusd @@@ Okay @@@ I need help ║
║ 4 ║ Ms. Kary ║ 2 ║ 1 ║ In each of the possibie equations, see if it is the same as 9 ( x + 2) = 90. @@@ i do not get this problem i am confusd @@@ Okay @@@ I need help ║
║ 5 ║ Beth ║ 4 ║ 2 ║ where's ms Q ║
║ 6 ║ Ms J ║ 3 ║ 2 ║ Not here today. will you please work with me ? @@@ where's ms Q ║
║ 7 ║ Beth ║ 4 ║ 2 ║ kk thats fine @@@ Not here today. will you please work with me ? @@@ where's ms Q ║
║ 8 ║ Ms J ║ 3 ║ 2 ║ what is 8 ÷ 10 written as a fraction @@@ kk thats fine @@@ Not here today. will you please work with me ? @@@ where's ms Q ║
╚═════════╩═════════════╩═══════════╩════════╩══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝
Notice that it is very similar to the first table but includes the discussion history.
Important data constraints:
- The tables are structured so that every odd chat_id value is a person of type A and every even chat_id value is a person of type B. So, the conversation always appears in the data in sequential order of A,B,A,B,..., and so on.) Cases where the conversation would be A,A or B,B at any point due to variable lengths of conversational sequences have been eliminated. So, A, A will never occur, and B, B will never occur.
- Every conversation has exactly 2 speakers. Cases where a different number of speakers existed for a given ConvID have been eliminated.
- I'm running SQL Server 2016.
So, how do I get to my desired table/view representation from where I'm starting?