有经验的老鸟。进来看看这个问题怎么解决!!
现在类似有套数据如下:
字段A|字段B|字段C
001, 002, a
001, 002, b
001, 002, c
002, 002, d
002, 002, e
003, 001, a
004, 002, b
要求得到如下结果:
001, 002, abc
002, 002, de
003, 001, a
004, 002, b
也就是对字段A字段B分组,同时在分组内。做字段C的拼接。
望解答。谢谢
问题点数:100、回复次数:12Top
1 楼NinGoo(http://www.NinGoo.net)回复于 2005-01-04 18:34:28 得分 10
oracle不清楚,给你个sql server的例子
eg2:
有表A,
id pid
1 1
1 2
1 3
2 1
2 2
3 1
如何化成表B:
id pid
1 1,2,3
2 1,2
3 1
或者是从表B变成A(不要用游标)
以前有相似的列子,现在找不到了,帮帮忙!
--1.创建一个合并的函数
create function fmerg(@id int)
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str=''
select @str=@str+','+cast(pid as varchar) from 表A where id=@id
set @str=right(@str,len(@str)-1)
return(@str)
End
go
--调用自定义函数得到结果
select distinct id,dbo.fmerg(id) from 表A
Top
2 楼ATGC(花开蝶舞,木秀鸟栖)回复于 2005-01-04 18:40:05 得分 0
不行,oracle做不到,除非你的字段C的值就那么固定几个,否则不行
只有写程序,这类例子论坛上很多的,可以自己找找
Top
3 楼NinGoo(http://www.NinGoo.net)回复于 2005-01-04 18:55:19 得分 0
oracle不行?那就导到sql server,做好了再导回来,哈哈Top
4 楼ATGC(花开蝶舞,木秀鸟栖)回复于 2005-01-04 18:59:32 得分 0
oracle你可以自己写个函数的Top
5 楼KarmenKing(卡门)回复于 2005-01-04 19:05:45 得分 0
有谁知道怎么发帖子阿
Top
6 楼skystar99047(天星)回复于 2005-01-04 19:07:29 得分 50
CREATE OR REPLACE function linkstr(tmp_c1 varchar2,tmp_c2 varchar2)
return varchar2
is
col_c3 varchar2(4000);
begin
for cur in (select c from table_name where a=tmp_c1 and b=tmp_c2) loop
col_c3:=col_c3||cur.c;
end loop;
col_c3:=rtrim(col_c3,1);
return col_c3;
end;
/
select a,b,linkstr(a,b) linkc from table_name group by a,b;Top
7 楼jgyun(小将)回复于 2005-01-04 20:05:55 得分 0
关注中...Top
8 楼tyrone98(林林)回复于 2005-01-04 20:50:15 得分 20
天星的是一种方法, 还有一种是在oracle9i以上才能用的,oracle9i可以加入聚合函数
create type CatStringImpl as object
(
catstring VARCHAR2(4000), -- 返回的字符串
static function ODCIAggregateInitialize(sctx IN OUT CatStringImpl)
return number,
member function ODCIAggregateIterate(self IN OUT CatStringImpl,
value IN varchar2) return number,
member function ODCIAggregateTerminate(self IN CatStringImpl,
returnValue OUT varchar2, flags IN number) return number,
member function ODCIAggregateMerge(self IN OUT CatStringImpl,
ctx2 IN CatStringImpl) return number
);
create or replace type body CatStringImpl is
static function ODCIAggregateInitialize(sctx IN OUT CatStringImpl )
return number is
begin
sctx := CatStringImpl('');
return ODCIConst.Success;
end;
member function ODCIAggregateIterate(self IN OUT CatStringImpl, value IN varchar2)
return number is
begin
self.catstring := self.catstring || ',' || value;
return ODCIConst.Success;
end;
member function ODCIAggregateTerminate(self IN CatStringImpl, returnValue OUT
varchar2, flags IN number) return number is
begin
returnValue := self.catstring;
return ODCIConst.Success;
end;
member function ODCIAggregateMerge(self IN OUT CatStringImpl , ctx2 IN
CatStringImpl ) return number is
begin
self.catstring = self.catstring || ',' || ctx2.catstring ;
return ODCIConst.Success;
end;
end;
建函数
create function catstr( input varchar2 ) return varchar2 PARALLEL_ENABLE AGGREGATE USING CatStringImpl ;
使用
select catstr( a) , b , c from tb group by b , c 就可以将字符串按照code组合起来.
Top
9 楼GerryYang(轻尘)回复于 2005-01-05 08:37:20 得分 0
学习.Top
10 楼Chinatosun(继续努力 UML系统分析 )回复于 2005-01-05 09:32:12 得分 0
学习.
Top
11 楼lynx(lynx)回复于 2005-01-05 11:37:25 得分 0
还是自己写函数吧,简单易用Top
12 楼hrui99(助人为本,潜水为行)回复于 2005-01-05 12:55:06 得分 20
create table test_haor (a char(3),b char(3),c char(1))
;
insert into test_haor
values('001','002','a');
insert into test_haor
values('001','002','b');
insert into test_haor
values('001','002','c');
insert into test_haor
values('002','002','d');
insert into test_haor
values('002','002','e');
insert into test_haor
values('003','001','a');
insert into test_haor
values('004','002','b');
commit;
set serveroutput on size 1000000
declare
union_c varchar2(20);
begin
for cura in (select distinct a,b from test_haor) loop
for cur in (select c from test_haor where a=cura.a and b=cura.b) loop
union_c:=union_c||cur.c;
end loop;
dbms_output.put_line(cura.a||' '||cura.b||' '||union_c);
union_c :='';
end loop;
end;
12:57:22 SQL> /
001 002 abc
002 002 de
003 001 a
004 002 b
Top




