注册 登录
编程论坛 SQL Server论坛

[求助]一个复杂的视图查询问题

ewilson 发布于 2007-04-04 15:30, 500 次点击

在一个数据中
(id、cname、job)分别代表字段

这是表中的内容:

id cname job
1 a b
1 a c
2 q e
用什么方法把这三项合成以下结果:
id cname job
1 a b,c
2 q e
或者在网页中输出表示出来
用什么方法?
请高手帮忙解决。最好能提供SQL语句

浙江省英锐特管理咨询公司
网络工程部:卢志坚
(台州高级人才网:www.tzgjrc.com)

1 回复
#2
棉花糖ONE2007-04-04 16:05

if object_id('test') is not null
drop table test
go
create table test(id int,cname char(1),job char(1))
insert into test select 1,'a','b'
union all select 1,'a','c'
union all select 2,'q','e'
go
select * from test
if object_id('dbo.fn') is not null
drop function dbo.fn
go
create function dbo.fn(@id int ,@cname char(1))
returns varchar(10)
as
begin
declare @t varchar(10)
select @t=' '
select @t=@t+job+',' from test where id=@id and cname=@cname
return left(@t,len(@t)-1)
end
go

select distinct id,cname,dbo.fn(id,cname) as job from test

1