英文:
How to combined String rows to one column
问题
data A | data B |
---|---|
apple | apple, banana, cat |
banana | apple, banana, cat |
cat | apple, banana, cat |
如果我有类似“data A”列的数据,并且我想将这些数据与另一列(如“data B”)合并,我可以如何查询并显示我想要的结果?
抱歉,我的SQL语句可能有点生疏。
英文:
data A | data B |
---|---|
apple | apple, banana, cat |
banana | apple, banana, cat |
cat | apple, banana, cat |
SELECT
data A,
'' as data B
From tbl_test;
If I have data like Column data A and I want to combined this data to another column like data b
How can I query this one and show like I want
Sorry for my news sql
答案1
得分: 0
通过使用STUFF
,我们可以通过编写子查询将所有列数据合并为单个列,适用于SQL Server 2014
。代码如下。
select dataA, STUFF((
select ',' + r.dataA
from TestTable as r
for xml path('')), 1, 1, '') as dataB From TestTable
英文:
By using STUFF
we can combine all column data into single column by writing Subquery it will give's Result to all columns suitable for SQL Server 2014
. Code is as follows.
select dataA,STUFF((
select ','+r.dataA
from TestTable as r
for xml path('')),1,1,'') as dataB From TestTable
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论