Oracle多行并一行(100分)

  • 主题发起人 主题发起人 say-rise
  • 开始时间 开始时间
S

say-rise

Unregistered / Unconfirmed
GUEST, unregistred user!
RULE_NO &nbsp; &nbsp; RULE_VALUE<br>1 170 OVER_SIZE_SIGN='N'<br>2 170 FREEZE_SIGN='N'<br>3 170 CNTR_OWNER='APL'<br>4 170 CNTR_TYPE='GP'<br>5 170 CNTR_SIZE='20'<br>6 170 DANGER_SIGN='N'<br>7 170 WHITHER='N'<br>8 170 WGT_DEGREE='N'<br>=======================================<br>并成一条记录<br>&nbsp;170 &nbsp; &nbsp; &nbsp; &nbsp;OVER_SIZE_SIGN='N' and FREEZE_SIGN='N' and CNTR_OWNER='APL' and CNTR_TYPE='GP' and CNTR_SIZE='20' and DANGER_SIGN='N' and WHITHER='N' and WGT_DEGREE='N'
 
这个应该很简单吧,你可以用RULE_NO来判断,如果相等,上一条记录就加到当前这条记录,然后把上一条记录的RULE_VALUE清空。最后把RULE_VALUE为空的记录delete。
 
只是写个SQL,不动表的数据。<br>最后把RULE_VALUE为空的记录delete???
 
如果用SQL,需要写自定义聚集函数,例如:<br><br>Oracle的定制功能十分强大。前几天在碰到一个问题的时候,查询文档发现,oracle不但允许用户定制自己的函数,还可以定制自己的聚集函数和分析函数。<br><br>下面是我使用自定义聚集函数建立一个字符串“sum”的小例子。<br><br>SQL&gt; CREATE OR REPLACE TYPE T_LINK AS OBJECT ( <br>&nbsp; 2 &nbsp;STR VARCHAR2(30000), <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 NUMBE<br>R) 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>&nbsp; 8 &nbsp;/ <br><br><br>类型已创建。<br><br><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;<br>&nbsp; 8 &nbsp;MEMBER FUNCTION ODCIAGGREGATEITERATE(SELF IN OUT T_LINK, VALUE IN VARCHAR2) RETURN NUMBER IS <br>&nbsp; 9 &nbsp;BEGIN <br>&nbsp;10 &nbsp;SELF.STR := SELF.STR || VALUE; <br>&nbsp;11 &nbsp;RETURN ODCICONST.SUCCESS; <br>&nbsp;12 &nbsp;END; <br>&nbsp;13 &nbsp; <br>&nbsp;14 &nbsp;MEMBER FUNCTION ODCIAGGREGATETERMINATE(SELF IN T_LINK, RETURNVALUE OUT VARCHAR2, FLAGS IN NUMBE<br>R) RETURN NUMBER IS <br>&nbsp;15 &nbsp;BEGIN <br>&nbsp;16 &nbsp;RETURNVALUE := SELF.STR; <br>&nbsp;17 &nbsp;RETURN ODCICONST.SUCCESS;<br>&nbsp;18 &nbsp;END; <br>&nbsp;19 &nbsp;<br>&nbsp;20 &nbsp;MEMBER FUNCTION ODCIAGGREGATEMERGE(SELF IN OUT T_LINK, CTX2 IN T_LINK) RETURN NUMBER IS <br>&nbsp;21 &nbsp;BEGIN <br>&nbsp;22 &nbsp;NULL; <br>&nbsp;23 &nbsp;RETURN ODCICONST.SUCCESS; <br>&nbsp;24 &nbsp;END; <br>&nbsp;25 &nbsp;END; <br>&nbsp;26 &nbsp;/ <br><br><br>类型主体已创建。<br><br><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><br>函数已创建。<br><br><br>SQL&gt; CREATE TABLE TEST (ID NUMBER, NAME VARCHAR2(20));<br><br><br>表已创建。<br><br><br>SQL&gt; INSERT INTO TEST VALUES (1, 'AAA');<br><br><br>已创建 1 行。<br><br><br>SQL&gt; INSERT INTO TEST VALUES (2, 'BBB');<br><br><br>已创建 1 行。<br><br><br>SQL&gt; INSERT INTO TEST VALUES (1, 'ABC');<br><br><br>已创建 1 行。<br><br><br>SQL&gt; INSERT INTO TEST VALUES (3, 'CCC');<br><br><br>已创建 1 行。<br><br><br>SQL&gt; INSERT INTO TEST VALUES (2, 'DDD');<br><br><br>已创建 1 行。<br><br><br>SQL&gt; COMMIT;<br><br><br>提交完成。<br><br><br>SQL&gt; COL NAME FORMAT A60<br><br><br>SQL&gt; SELECT ID, F_LINK(NAME) NAME FROM TEST GROUP BY ID;<br><br><br>&nbsp; &nbsp; &nbsp; &nbsp; ID NAME<br>---------- ------------------------------------------------------<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1 AAAABC<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2 BBBDDD<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;3 CCC
 

Similar threads

I
回复
0
查看
625
import
I
S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
900
SUNSTONE的Delphi笔记
S
D
回复
0
查看
767
DelphiTeacher的专栏
D
D
回复
0
查看
809
DelphiTeacher的专栏
D
后退
顶部