Oracle custom Aggregate functions

Source: Internet
Author: User

-- create a function type
Create type strcat_type as object (
cat_string varchar2 (4000),
-- set the initialization of a UDF, start a clustering function
static function odciaggregateinitialize (cs_ctx in out strcat_type) return number,
-- custom clustering function, the main step, this function defines what the aggregate function performs. In the following example, the maximum value, minimum value, average value, or connection operation is performed. self is the pointer to the current aggregate function, used to associate with the previous calculation results
member function odciaggregateiterate (self in out strcat_type, value in varchar2) return number,
-- used to merge the results of two different pointers of two Aggregate functions. You can merge the data of different results, especially when processing parallel (parallel) queries of Aggregate functions.
member function odciaggregatemerge (self in out strcat_type, ctx2 in out strcat_type) return number,
-- terminate the processing of clustering functions, returns the result of processing the aggregate function.
member function odciaggresponterminate (self in out strcat_type, returnvalue out varchar2, flags in number) return number
)
--/*
-- function subject

*/
Create or replace type body strcat_type is
Static function odciaggresponinitialize (cs_ctx in out strcat_type) return number
Is
Begin
Cs_ctx: = strcat_type (null );
Return odciconst. success;
End;

Member function odciaggregateiterate (self in out strcat_type,
Value in varchar2)
Return number
Is
Begin
Self. cat_string: = self. cat_string | value;
-- 2.get Union set
-- If instr (self. cat_string, value) = 0 or self. cat_string is null then
-- Self. cat_string: = self. cat_string | ',' | value;
-- Else
-- Self. cat_string: = self. cat_string | '';
-- End if;
Return odciconst. success;
End;

member function odciaggresponterminate (self in out strcat_type,
returnvalue out varchar2,
flags in number)
return number
is
begin
returnvalue: = ltrim (rtrim (self. cat_string, ','), ',');
return odciconst. success;
end;

Member function odciaggregatemerge (self in out strcat_type,
Ctx2 in out strcat_type)
Return number
Is
Begin
Self. cat_string: = self. cat_string | ',' | ctx2.cat _ string;
Return odciconst. success;
End;
End;

/*
Function call

*/

Create or replace
Function strcat (input varchar2)
Return varchar2
Parallel_enable aggregate using strcat_type;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.