SQL Server 中的连接组 [英] Concat groups in SQL Server

查看:29
本文介绍了SQL Server 中的连接组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一张这样的桌子:

If I have a table like this:

+------------+
| Id | Value |
+------------+
| 1  | 'A'   |
|------------|
| 1  | 'B'   |
|------------|
| 2  | 'C'   |
+------------+

我怎样才能得到这样的结果集:

How can I get a resultset like this:

+------------+
| Id | Value |
+------------+
| 1  | 'AB'  |
|------------|
| 2  | 'C'   |
+------------+

我知道这在 MySQL 中使用 GROUP_CONCAT 很容易做到,但我需要能够在 MSSQL 2005 中做到这一点

I know this is really easy to do in MySQL using GROUP_CONCAT, but I need to be able to do it in MSSQL 2005

谢谢

(重复 如何使用 GROUP BY在 SQL Server 中连接字符串?)

推荐答案

要获得干净高效的解决方案,您可以创建 一个用户定义的聚合函数,甚至还有一个例子 可以满足您的需求.
然后,您可以像使用任何其他聚合函数一样使用它(使用标准查询计划):

For a clean and efficient solution you can create an user defined aggregate function, there is even an example that does just what you need.
You can then use it like any other aggregate function (with a standard query plan):

这篇关于SQL Server 中的连接组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆