本文不打算开始讨论SQL Server中字符串的串联最佳方法,因为许多文章已经这样做了。 我想对组连接片本身使用这样的方法:https://groupconcat.codeplex.com/但我对其他选项持开放态度。 问题是这样的:我需要看看会议的历史。假设我有这样的数据,可以根据需要添加更多ID:
╔═════════╦═════════════╦═══════════╦════════╦══════════════════════════════════════════════════════════════════════════════╗
║ 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 ║
╚═════════╩═════════════╩═══════════╩════════╩══════════════════════════════════════════════════════════════════════════════╝
使用group concat函数会导致数据看起来像这样(注意我使用“@@@”作为分隔符以使其更易于阅读):
╔════════╦══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗
║ 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 ║
╚════════╩══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝
我需要的是:
╔═════════╦═════════════╦═══════════╦════════╦══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗
║ 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 ║
╚═════════╩═════════════╩═══════════╩════════╩══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝
请注意,它与第一个表非常相似,但包含讨论历史记录。
重要的数据限制:
那么,我如何从我开始的地方到达我想要的表/视图表示?
答案 0 :(得分:1)
我认为你可以使用stuff和xml路径如下:
Select *,
stuff((select ' @@@ ' + [Text] from #Speaker where s.convid = convid and chat_id <= s.chat_id order by chat_id desc for xml path('')),1,5,'') as ConcatenatedText
from #Speaker s
输出如下:
+--------------------------------------------------------------------------------------------------------------------------------------------------+---------+-------------+-----------+--------+------------------------------------------------------------------------------+
| ConcatenatedText | chat_id | SpeakerName | SpeakerId | convid | text |
+--------------------------------------------------------------------------------------------------------------------------------------------------+---------+-------------+-----------+--------+------------------------------------------------------------------------------+
| I need help | 1 | Ruby | 1 | 1 | I need help |
| Okay @@@ I need help | 2 | Ms. Kary | 2 | 1 | Okay |
| i do not get this problem i am confusd @@@ Okay @@@ I need help | 3 | Ruby | 1 | 1 | i do not get this problem i am confusd |
| 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 | 4 | Ms. Kary | 2 | 1 | In each of the possibie equations, see if it is the same as 9 ( x + 2) = 90. |
| where's ms Q | 5 | Beth | 4 | 2 | where's ms Q |
| Not here today. will you please work with me ? @@@ where's ms Q | 6 | Ms J | 3 | 2 | Not here today. will you please work with me ? |
| kk thats fine @@@ Not here today. will you please work with me ? @@@ where's ms Q | 7 | Beth | 4 | 2 | kk thats fine |
| what is 8 ÷ 10 written as a fraction @@@ 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 |
+--------------------------------------------------------------------------------------------------------------------------------------------------+---------+-------------+-----------+--------+------------------------------------------------------------------------------+