---第一种 select * from( select distinct column1 from table1 )a cross apply( select column2 = ( select [*] = column2 from table1 where column1 = a.column1 for xml path(''), type ).value('/', 'nvarchar(20)') )b ----第二种 select *from(selec
---第一种
select * from( select distinct column1 from table1 )a cross apply( select column2 = ( select [*] = column2 from table1 where column1 = a.column1 for xml path(''), type ).value('/', 'nvarchar(20)') )b ----第二种 select *from(select distinct column1 from table1)a
outer apply(select [values]= stuff(replace(replace((select column2 from table1 n
where column1 = a.column1 for xml auto), '', ''), 1, 1, '')
)nselect * from table1select * from b---------------------------------------------------另外的方法create function dbo.f_str(@id int)
returns varchar(8000)
as
begin
declare @r varchar(8000)
set @r = ''
select @r = @r + ',' + value
from tb
where id=@id
return stuff(@r, 1, 1, '')
end
go
-- 调用函数
select id, values=dbo.f_str(id)
from tb
group by id
-- 2. 新的解决方法
-- 示例数据
declare @t table(id int, value varchar(10))
insert @t select 1, 'aa'
union all select 1, 'bb'
union all select 2, 'aaa'
union all select 2, 'bbb'
union all select 2, 'ccc'
-- 查询处理
select *
from(
select distinct
id
from @t
)a
outer apply(
select
[values]= stuff(replace(replace(
(
select value from @t n
where id = a.id
for xml auto
), '', ''), 1, 1, '')
)n
/*--结果
idvalues
----------- ----------------
1aa,bb
2aaa,bbb,ccc
(2 行受影响)
--*/
--各种字符串分函数
--3.3.1 使用游标法进行字符串合并处理的示例。
--处理的数据
create table tb(col1 varchar(10),col2 int)
insert tb select 'a',1
union all select 'a',2
union all select 'b',1
union all select 'b',2
union all select 'b',3
--合并处理
--定义结果集表变量
declare @t table(col1 varchar(10),col2 varchar(100))
--定义游标并进行合并处理
declare tb cursor local
for
select col1,col2 from tb order by col1,col2
declare @col1_old varchar(10),@col1 varchar(10),@col2 int,@s varchar(100)
open tb
fetch tb into @col1,@col2
select @col1_old=@col1,@s=''
while @@fetch_status=0
begin
if @col1=@col1_old
select @s=@s+','+cast(@col2 as varchar)
else
begin
insert @t values(@col1_old,stuff(@s,1,1,''))
select @s=','+cast(@col2 as varchar),@col1_old=@col1
end
fetch tb into @col1,@col2
end
insert @t values(@col1_old,stuff(@s,1,1,''))
close tb
deallocate tb
--显示结果并删除测试数据
select * from @t
drop table tb
/*--结果
col1col2
---------- -----------
a1,2
b1,2,3
--*/
go
/*==============================================*/
--3.3.2 使用用户定义函数,美国空间,配合select处理完成字符串合并处理的示例
--处理的数据
create table tb(col1 varchar(10),col2 int)
insert tb select 'a',1
union all select 'a',2
union all select 'b',1
union all select 'b',2
union all select 'b',3
go
--合并处理函数
create function dbo.f_str(@col1 varchar(10))
returns varchar(100)
as
begin
declare @re varchar(100)
set @re=''
select @re=@re+','+cast(col2 as varchar)
from tb
where col1=@col1
return(stuff(@re,1,1,''))
end
go
--调用函数
select col1,col2=dbo.f_str(col1) from tb group by col1
--删除测试
drop table tb
drop function f_str
/*--结果
col1col2
---------- -----------
a1,2
b1,2,3
--*/
go
------------------------------------------
--3.3.3 使用临时表实现字符串合并处理的示例
--处理的数据
create table tb(col1 varchar(10),col2 int)
insert tb select 'a',1
union all select 'a',2
union all select 'b',1
union all select 'b',2
union all select 'b',3
--合并处理
select col1,col2=cast(col2 as varchar(100))
into #t from tb
order by col1,col2
declare @col1 varchar(10),@col2 varchar(100)
update #t set
@col2=case when @col1=col1 then @col2+','+col2 else col2 end,
@col1=col1,
col2=@col2
select * from #t
/*--更新处理后的临时表
col1col2
---------- -------------
a1
a1,2
b1
b1,2
b1,2,3
--*/
--得到最终结果
select col1,col2=max(col2) from #t group by col1
/*--结果
col1col2
---------- -----------
a1,2
b1,2,3
--*/
--删除测试
drop table tb,#t
go
/*==============================================*/
--3.3.4.1 每组 --处理的数据
create table tb(col1 varchar(10),col2 int)
insert tb select 'a',1
union all select 'a',2
union all select 'b',1
union all select 'b',2
union all select 'c',3
--合并处理
select col1,
col2=cast(min(col2) as varchar)
+case
when count(*)=1 then ''
else ','+cast(max(col2) as varchar)
end
from tb
group by col1
drop table tb
/*--结果
col1col2
---------- ----------
a1,2
b1,2
c3
--*/