Oracle,查询出来的结果集怎么按关键字合并???谢谢(100分)

  • 主题发起人 主题发起人 himoo
  • 开始时间 开始时间
H

himoo

Unregistered / Unconfirmed
GUEST, unregistred user!
现在有个问题:<br>我用select * from table 之后出来一个结果集:<br>COL1 COL2 COL3 ...<br>A &nbsp; &nbsp;A1 &nbsp; A2 &nbsp; ...<br>A &nbsp; &nbsp;A3 &nbsp; A4 &nbsp; ...<br>B &nbsp; &nbsp;B1 &nbsp; B2 &nbsp; ...<br>B &nbsp; &nbsp;B3 &nbsp; B4 &nbsp; ...<br>.<br>.<br>.<br><br>但我的要求是最终的结果集是这样的:<br>A &nbsp; A1;A3 &nbsp; A2;A4<br>B &nbsp; B1;B3 &nbsp; B2;B4<br>...<br><br>就是按照COL1 来建立结果集,其它字段自动合并。Oracle数据库,请问怎么实现这个最终的结果集?谢谢。
 
使用光标。。。
 
要具体的代码~~我不太会这个,谢谢。
 
纯粹的sql输出结果不可能实现,除非用过程输出或者将结果写入临时表
 
同意2楼,写存储过程或函数,用cursor遍历结果集,自己合并
 
自定义聚集函数<br><br>SQL&gt; CREATE OR REPLACE TYPE T_LINK AS OBJECT (<br> &nbsp;2 &nbsp;STR VARCHAR2(4000),<br> &nbsp;3 &nbsp;STATIC FUNCTION ODCIAGGREGATEINITIALIZE(SCTX IN OUT T_LINK) RETURN NUMBER,<br> &nbsp;4 &nbsp;MEMBER FUNCTION ODCIAGGREGATEITERATE(SELF IN OUT T_LINK, VALUE IN VARCHAR2) RETURN NUMBER,<br> &nbsp;5 &nbsp;MEMBER FUNCTION ODCIAGGREGATETERMINATE(SELF IN T_LINK, RETURNVALUE OUT VARCHAR2, FLAGS IN NUMBER) RETURN NUMBER,<br> &nbsp;6 &nbsp;MEMBER FUNCTION ODCIAGGREGATEMERGE(SELF IN OUT T_LINK, CTX2 IN T_LINK) RETURN NUMBER )<br> &nbsp;7 &nbsp;/<br><br>Type created<br><br>SQL&gt; <br>SQL&gt; CREATE OR REPLACE TYPE BODY T_LINK IS<br> &nbsp;2 &nbsp;STATIC FUNCTION ODCIAGGREGATEINITIALIZE(SCTX IN OUT T_LINK) RETURN NUMBER IS<br> &nbsp;3 &nbsp;BEGIN<br> &nbsp;4 &nbsp;SCTX := T_LINK(NULL);<br> &nbsp;5 &nbsp;RETURN ODCICONST.SUCCESS;<br> &nbsp;6 &nbsp;END;<br> &nbsp;7 &nbsp;MEMBER FUNCTION ODCIAGGREGATEITERATE(SELF IN OUT T_LINK, VALUE IN VARCHAR2) RETURN NUMBER IS<br> &nbsp;8 &nbsp;BEGIN<br> &nbsp;9 &nbsp;IF SELF.STR IS NULL THEN<br> 10 &nbsp; &nbsp; SELF.STR:=VALUE;<br> 11 &nbsp;ELSE<br> 12 &nbsp; &nbsp; SELF.STR := SELF.STR || ';' || VALUE;<br> 13 &nbsp;END IF;<br> 14 &nbsp;RETURN ODCICONST.SUCCESS;<br> 15 &nbsp;END;<br> 16 &nbsp;MEMBER FUNCTION ODCIAGGREGATETERMINATE(SELF IN T_LINK, RETURNVALUE OUT VARCHAR2, FLAGS IN NUMBER) RETURN NUMBER IS<br> 17 &nbsp;BEGIN<br> 18 &nbsp;RETURNVALUE := SELF.STR;<br> 19 &nbsp;RETURN ODCICONST.SUCCESS;<br> 20 &nbsp;END;<br> 21 &nbsp;MEMBER FUNCTION ODCIAGGREGATEMERGE(SELF IN OUT T_LINK, CTX2 IN T_LINK) RETURN NUMBER IS<br> 22 &nbsp;BEGIN<br> 23 &nbsp;NULL;<br> 24 &nbsp;RETURN ODCICONST.SUCCESS;<br> 25 &nbsp;END;<br> 26 &nbsp;END;<br> 27 &nbsp;/<br><br>Type body created<br><br>SQL&gt; <br>SQL&gt; CREATE OR REPLACE FUNCTION F_LINK(P_STR VARCHAR2) RETURN VARCHAR2<br> &nbsp;2 &nbsp;AGGREGATE USING T_LINK;<br> &nbsp;3 &nbsp;/<br><br>Function created<br><br>SQL&gt; <br>SQL&gt; INSERT INTO test VALUES('A','A1','A2');<br><br>1 row inserted<br><br>SQL&gt; INSERT INTO test VALUES('A','A3','A4');<br><br>1 row inserted<br><br>SQL&gt; INSERT INTO test VALUES('B','B1','B2');<br><br>1 row inserted<br><br>SQL&gt; INSERT INTO test VALUES('B','B3','B4');<br><br>1 row inserted<br><br>SQL&gt; SELECT col1,f_link(col2),f_link(col3) FROM test GROUP BY col1;<br><br>COL1 &nbsp; &nbsp; &nbsp; F_LINK(COL2) &nbsp; &nbsp; &nbsp; &nbsp;F_LINK(COL3)<br>---------- ------------ ---------------------<br>A &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;A1;A3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; A2;A4<br>B &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;B1;B3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; B2;B4
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=3640002
 
接受答案了.
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
I
回复
0
查看
548
import
I
I
回复
0
查看
693
import
I
I
回复
0
查看
679
import
I
后退
顶部