tableA表的数据如下
ID Name
1 aaa
1 bbb
1 ccc
2 aaa
2 bbb
2 ccc如何得到下面数据:
ID Name
1 aaa,bbb,ccc
2 aaa,bbb,ccc即按ID把Name合并,用最简单的方法。
请教各位,thanks!
ID Name
1 aaa
1 bbb
1 ccc
2 aaa
2 bbb
2 ccc如何得到下面数据:
ID Name
1 aaa,bbb,ccc
2 aaa,bbb,ccc即按ID把Name合并,用最简单的方法。
请教各位,thanks!
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-09-10 17:07:45
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([ID] int,[Name] varchar(3))
insert [tb]
select 1,'aaa' union all
select 1,'bbb' union all
select 1,'ccc' union all
select 2,'aaa' union all
select 2,'bbb' union all
select 2,'ccc'
--------------开始查询--------------------------
select id, [name]=stuff((select ','+[name] from tb t where id=tb.id for xml path('')), 1, 1, '')
from tb
group by id
----------------结果----------------------------
/* id name
----------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 aaa,bbb,ccc
2 aaa,bbb,ccc(2 行受影响)*/
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2009-09-10 17:07:45
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([ID] int,[Name] varchar(3))
insert [tb]
select 1,'aaa' union all
select 1,'bbb' union all
select 1,'ccc' union all
select 2,'aaa' union all
select 2,'bbb' union all
select 2,'ccc'
--------------开始查询--------------------------
CREATE FUNCTION dbo.f_strUnite(@id int)
RETURNS varchar(8000)
AS
BEGIN
DECLARE @str varchar(8000)
SET @str = ''
SELECT @str = @str + ',' + [name] FROM tb WHERE id=@id
RETURN STUFF(@str, 1, 1, '')
END
GO
-- 调用函数
SELECt id, [name] = dbo.f_strUnite(id) FROM tb GROUP BY id
drop table tb
drop function dbo.f_strUnite
go
----------------结果----------------------------
/* id name
----------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 aaa,bbb,ccc
2 aaa,bbb,ccc(2 行受影响)*/
go
create table tb(id int,name varchar(10))
insert tb
select
1, 'aaa' union all select
1, 'bbb' union all select
1, 'ccc' union all select
2, 'aaa' union all select
2, 'bbb' union all select
2, 'ccc'
if object_id('f_tb2') is not null drop function f_tb2
go
create function f_tb2(@id int)
returns nvarchar(100)
as
begin
declare @sql nvarchar(4000)
set @sql=N''
select @sql=@sql+N','+name from tb where id=@id
set @sql=stuff(@sql,1,1,N'')
return(@sql)
end
goselect ID,dbo.f_tb2(id) as 结果 from tb group by id/*
ID 结果
1 aaa,bbb,ccc
2 aaa,bbb,ccc
*/