我想为group by语句中的聚合列的不同事件生成逗号分隔的字符串。所以我有:
Select Column1, ???statement involving Column2??? from MyTable
group by Column1
我无法弄清问问题之间的内容。
答案 0 :(得分:0)
假设您有以下表格:
use TSQL2012
if object_id('testStackOverflow') is not null drop table testStackOverflow
create table testStackOverflow(
column1 varchar(100) not null,
column2 varchar(100) not null,
constraint PK2 primary key(column1,column2)
)
insert into testStackOverflow(column1,column2)
values
('value1','t1'),
('value1','t2'),
('value1','t3'),
('value2','t5'),
('value2','t6'),
('value2','t7')
您可以使用以下查询:
Select column1,
substring(
(
Select ','+sto2.column2 AS [text()]
From testStackOverflow as sto2
Where sto2.column1 = sto1.column1
ORDER BY sto2.column1
For XML PATH ('')
), 2, 1000) as column2concat
From testStackOverflow as sto1
group by column1
结果将是:
我使用Ritesh的回答作为参考: https://code.google.com/p/gdata-issues/issues/detail?id=4294