在SQL Server中使用历史记录组连接

时间:2017-07-28 18:05:35

标签: sql sql-server database sql-server-2016 sqlclr

本文不打算开始讨论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                      ║
╚═════════╩═════════════╩═══════════╩════════╩══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝

请注意,它与第一个表非常相似,但包含讨论历史记录。

重要的数据限制:

  • 这些表的结构使得每个奇怪的chat_id值都是一个人 类型A 每个偶数chat_id值都是B类型的人。所以 会话总是按顺序出现在数据中 A,B,A,B,......等等。)会话的对象是A,A或 由于会话序列的长度不同,B,B在任何时候都是如此 已被淘汰。所以,A,A永远不会发生,B,B永远不会发生 发生。
  • 每次谈话都有两位发言者。案例在哪里 给定的ConvID存在不同数量的发言者 消除。
  • 我正在运行SQL Server 2016。

那么,我如何从我开始的地方到达我想要的表/视图表示?

1 个答案:

答案 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                                         |
+--------------------------------------------------------------------------------------------------------------------------------------------------+---------+-------------+-----------+--------+------------------------------------------------------------------------------+

Demo

相关问题