gpt4 book ai didi

sql - 逗号分隔结果多列sql server

转载 作者:行者123 更新时间:2023-12-02 22:31:08 25 4
gpt4 key购买 nike

我有一个包含值的表:

Key1     Key2     ColumnKey 
============================
1 idx1 here
2 idx2 there

我需要返回,因为不止一列结果用逗号分隔。

示例:

1,2   idx1,idx2,      here,there

最佳答案

select stuff(T.X.query('Key1').value('.', 'varchar(max)'), 1, 1, '') as Key1,
stuff(T.X.query('Key2').value('.', 'varchar(max)'), 1, 1, '') as Key2,
stuff(T.X.query('ColumnKey').value('.', 'varchar(max)'), 1, 1, '') as ColumnKey
from
(
select ','+cast(Key1 as varchar(10)) as Key1,
','+Key2 as Key2,
','+ColumnKey as ColumnKey
from YourTable
order by Key1
for xml path(''), type
) T(X)

SE-Data

关于sql - 逗号分隔结果多列sql server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12225563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com