ORACLE 自定义聚合函数
2016-05-31 20:31
357 查看
用户可以自定义聚合函数ODCIAggregate,定义了四个聚集函数:初始化、迭代、合并和终止。
InitializationisaccomplishedbytheODCIAggregateInitialize()routine,whichisinvokedbyOracletoinitializethecomputationoftheuser-definedaggregate.TheinitializedaggregationcontextispassedbacktoOracleasanobjecttypeinstance.
IterationisperformedthroughtheODCIAggregateIterate()routine,whichisrepeatedlyinvokedbyOracle.Oneachinvocation,anewvalueorasetofnewvaluesandthecurrentaggregationcontextarepassedin.Theroutineprocessesthenewvaluesandreturnstheupdatedaggregationcontext.Thisroutineisinvokedforeverynon-
MergingisperformedbyODCIAggregateMerge(),aroutineinvokedbyOracletocombinetwoaggregationcontexts.Thisroutinetakesthetwocontextsasinputs,combinesthem,andreturnsasingleaggregationcontext.
TerminationtakesplacewhentheODCIAggregateTerminate()routineisinvokedbyOracleasthefinalstepofaggregation.Theroutinetakestheaggregationcontextasinputandreturnstheresultingaggregatevalue.
Considertheaggregatefunction
Toperformthiscomputation,theaggregatefunction
Initializesthecomputationbyinitializingtheaggregationcontext,ortherowsoverwhichaggregationisperformed:
Iterativelyprocesseseachsuccessiveinputvalueandupdatesthecontext:
[Optional]Mergebycombiningthetwoaggregationcontextsandreturnasinglecontext.Thisoperationcombinestheresultsofaggregationoversubsetstoobtaintheaggregateovertheentireset.Thisextrastepcanberequiredduringeitherserialorparallelevaluationofanaggregate.Ifneeded,itisperformedbeforestep4:
Section"EvaluatingUser-DefinedAggregatesinParallel"describesthisstepingreaterdetail.
Terminatesbycomputingtheresult;usesthecontexttoreturntheresultantaggregatevalue:
If
Example12-2ImplementingtheODCIAggregateInterface
The
Example12-3DefiningaUser-DefinedAggregateFunction
Thisfunctiondefinitioncreatesthe
Initializationisaccomplishedbythe
Iterationisperformedthroughthe
NULLvalueintheunderlyinggroup.
NULLvaluesareignoredduringaggregationandarenotpassedtotheroutine.
Mergingisperformedby
Terminationtakesplacewhenthe
Considertheaggregatefunction
AVG()inthefollowingstatement:
SELECTAVG(T.Sales) FROMAnnualSalesT GROUPBYT.State;
Toperformthiscomputation,theaggregatefunction
AVG()goesthroughthesesteps:
Initializesthecomputationbyinitializingtheaggregationcontext,ortherowsoverwhichaggregationisperformed:
runningSum=0;runningCount=0;
Iterativelyprocesseseachsuccessiveinputvalueandupdatesthecontext:
runningSum+=inputval;runningCount++;
[Optional]Mergebycombiningthetwoaggregationcontextsandreturnasinglecontext.Thisoperationcombinestheresultsofaggregationoversubsetstoobtaintheaggregateovertheentireset.Thisextrastepcanberequiredduringeitherserialorparallelevaluationofanaggregate.Ifneeded,itisperformedbeforestep
runningSum=runningSum1+runningSum2; runningCount=runningCount1+runningCount2
Section
Terminatesbycomputingtheresult;usesthecontexttoreturntheresultantaggregatevalue:
return(runningSum/runningCount);
If
AVG()wereauser-definedfunction,theobjecttypethatembodiesitwouldimplementamethodforacorresponding
ODCIAggregateroutineforeachofthesesteps.Thevariables
runningSumand
runningCount,whichdeterminethestateoftheaggregationintheexample,wouldbeattributesofthatobjecttype.
Example12-2ImplementingtheODCIAggregateInterface
The
ODCIAggregateroutinesareimplementedasmethodswithinanobjecttype
SpatialUnionRoutines.TheactualimplementationcouldbeinanyOracle-supportedlanguagefortypemethods,suchasPL/SQL,C,C++orJava.
CREATETYPESpatialUnionRoutines( STATICFUNCTIONODCIAggregateInitialize(...)..., MEMBERFUNCTIONODCIAggregateIterate(...)..., MEMBERFUNCTIONODCIAggregateMerge(...)..., MEMBERFUNCTIONODCIAggregateTerminate(...) ); CREATETYPEBODYSpatialUnionRoutinesIS ... END;
Example12-3DefiningaUser-DefinedAggregateFunction
Thisfunctiondefinitioncreatesthe
SpatialUnion()aggregatefunctionbyspecifyingitssignatureandtheobjecttypethatimplementsthe
ODCIAggregateinterface:
CREATEFUNCTIONSpatialUnion(xGeometry)RETURNGeometry AGGREGATEUSINGSpatialUnionRoutines;
http://docs.oracle.com/database/121/ADDCI/ext_agg_ref.htm#ADDCI5129
相关文章推荐
- Oracle 11g安装一
- Oracle数据使用中出现的几种问题及解决方法
- ORACLE命令笔记
- Oracle存储过程基本语法介绍
- Oracle和Mysqld的时间函数
- ORACLE LOCK MODE 1.2.3.4.5.6
- CentOS 6.4 X64 利用 yum 升级到 Oracle linux 6.4 内核
- Oracle11g结合ArcGIS10.2建立空间数据库ST_Geometry类库配置问题
- Oracle EXP-00091解决方法
- oracle修改分区表
- Powerdesigner逆向工程
- Oracle MySQL
- oracle导入数据库报错:IMP-00019: 由于 ORACLE 错误 12899 而拒绝行 IMP-00003: 遇到 ORACLE 错误 12899
- ORACLE 11g EM 网页显示按钮上的文字为乱码
- Oracle ACFS Support on Linux詳解
- Oracle 数据库的对象定义操作(DDL语句)
- Oracle查询被锁定表以及解锁
- oracle表空间
- oracle分区表备份恢复
- .net操作Oracle数据库步骤及方法