日期:2014-05-19  浏览次数:20449 次

在SQL2000中进行行列的转换
原表
pno           type     goods1     goods2
  1               2           A                 C
  1               2           B                 C
  2               1           D                 E
  3               3           F                 G
  3               3           F                 H  
  3               3           F                 J

我想转换成新表
pno     type     goods1   goods2
1           2           A,B           C
2           1             D             E
3           3             F           G,H,J

说明:就是按照pno分类,相同的pno,goods1,goods进行横向合并

哪位能替我解难,小弟万分感谢

------解决方案--------------------
create function test_f(@pno int,@type int)
returns nvarchar(100)
as
begin
declare @s nvarchar(100)
select @s=isnull(@s+ ', ', ' ')+goods1 from table1 where pno=@pno and type=@type group by goods1
return @s
end

go
create function test_f2(@pno int,@type int)
returns nvarchar(100)
as
begin
declare @s nvarchar(100)
select @s=isnull(@s+ ', ', ' ')+goods2 from table1 where pno=@pno and type=@type group by goods2
return @s
end
go
select distinct pno , type ,goods1=dbo.test_f(pno , type),goods2 =dbo.test_f2(pno , type) from table1