group by 有关问题

求助group by 问题
id     name
1       zhansan
2       zhansan
3       lisi
4       lisi
5       zhangsan

请问如何按name查询出各个name所包含的id?
例如结果
id                           name
1,2,5                 zhansan
3,4                       lisi

用group   by吗?用什么聚合函数?

------解决方案--------------------
create function getV(@name varchar(20))
returns varchar(100)
as
begin
declare @a varchar(100)
select @a=isnull(@a+ ', ', ' ')+ltrim(id) from [Table] where name=@name
return @a
end
go
select dbo.getV(name) id,name group by name
------解决方案--------------------
用group by吗?用什么聚合函数?
---------
自己写个函数吧,,,
------解决方案--------------------
--带符号合并行列转换

--有表t,其数据如下:
a b
1 1
1 2
1 3
2 1
2 2
3 1
--如何转换成如下结果:
a b
1 1,2,3
2 1,2
3 1

create table tb
(
a int,
b int
)
insert into tb(a,b) values(1,1)
insert into tb(a,b) values(1,2)
insert into tb(a,b) values(1,3)
insert into tb(a,b) values(2,1)
insert into tb(a,b) values(2,2)
insert into tb(a,b) values(3,1)
go

if object_id( 'pubs..f_hb ') is not null
drop function f_hb
go

--创建一个合并的函数
create function f_hb(@a int)
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str = ' '
select @str = @str + ', ' + cast(b as varchar) from tb where a = @a
set @str = right(@str , len(@str) - 1)
return(@str)
End
go

--调用自定义函数得到结果:
select distinct a ,dbo.f_hb(a) as b from tb

drop table tb

--结果
a b
----------- ------
1 1,2,3
2 1,2
3 1

(所影响的行数为 3 行)
------解决方案--------------------
--测试数据
create table test(id int,name varchar(8))
insert test
select 1, 'zhansan ' union all
select 2, 'zhansan ' union all
select 3, 'lisi ' union all
select 4, 'lisi ' union all
select 5, 'zhangsan '
go

--处理函数
create function fn_test(@name varchar(8))
returns varchar(1000)
as
begin
declare @return varchar(1000)
select @return = coalesce(@return + ', ', ' ') + ltrim(id) from test where name=@name
return(@return)
end
go

--调用函数汇总
select id=dbo.fn_test(name),name from test group by name
/*
id name
3,4 lisi
5 zhangsan
1,2 zhansan
*/

--删除测试
drop table test
drop function fn_test

------解决方案--------------------
create table tb (id int,name varchar(10))
insert into tb values(1, 'zhansan ')
insert into tb values(2, 'zhansan ')
insert into tb values(3, 'lisi ')
insert into tb values(4, 'lisi ')
insert into tb values(5, 'zhangsan ')
go

--创建一个合并的函数
create function f_hb(@name varchar(10))