行的串联 [英] Concatenation of Rows

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

问题描述



当我尝试连接几行时出现问题.该程序只是不断添加相同的数据,而已存在的次数.例如,如果我想将6行连接成1,并且每行都包含如下内容:

Hi,

I have a problem when I try to concatenate few rows. The program just keep adding the same data the number of times that it exist. For example if I want to concatenate 6 rows into 1, and each rows contain something like this:

Alabama : Huntsville
California : Los Angeles
Miami : Florida
Michigan : Detroit
Ohio : Cleveland
Texas : Houston


(这不是真实数据,仅是示例)
它正在执行以下操作:
行状态:大写
1阿拉巴马州:加利福尼亚州亨茨维尔:洛杉矶洛杉矶,迈阿密:佛罗里达州,
密歇根州:俄亥俄州底特律:德克萨斯州克利夫兰:阿拉巴马州休斯敦:
加利福尼亚州亨茨维尔:洛杉矶,等等...

我正在使用CROSS APPLY,例如:


(This is not the real data is just an example)
It is doing the following:
Row State:Capital
1 Alabama : Huntsville, California : Los Angeles, Miami : Florida,
Michigan : Detroitm, Ohio : Cleveland, Texas : Houston, Alabama :
Huntsville, California : Los Angeles, and so on...

I am using CROSS APPLY, an using as example this:

SELECT DISTINCT CategoryId, ProductNames
FROM Northwind.dbo.Products p1
CROSS APPLY ( SELECT ProductName + ',';
FROM Northwind.dbo.Products p2
WHERE p2.CategoryId = p1.CategoryId
ORDER BY ProductName
FOR XML PATH('') ) D ( ProductNames )

推荐答案

看看以下链接:

http://stackoverflow.com/questions/194852/in-sql-server-how-do-you-concatenate-many-rows-into-a-single-text-str [
Take a look at this link:

http://stackoverflow.com/questions/194852/in-sql-server-how-do-you-concatenate-many-rows-into-a-single-text-str[^]

Good luck!


这篇关于行的串联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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