My company has just made the switch from MySQL to Oracle. One of the issues that I have encountered is the lack of a string aggregation function; in MySQL there is the GROUP_CONCAT().
Basically, it will retrieve a list of all of the pertinent data contained in the selected column.
For instance, in MySQL:
This also automatically orders the column.
-Fine, back to Oracle ...
I found this excellent function on the web to and merely renamed the function 'GROUP_CONCAT'. However, with the same table and query- I get the following results:
The function pulls the data out of the table exactly as it is ordered. Unfortunately, order is important and I have to have this list ordered in the same fashion as the MySQL function does.
Here is the link for the function I created:
And here is the code:
I am unfamiliar with writing custom functions inside the dbms; let alone for Oracle.
Any thoughts or suggestions on how I can best achieve an ordered list based on the code above? Any input is greatly appreciated.
Thanks in advance,
Mike
[blue]Go to work to learn. Don't go to work to earn.[/blue]
Basically, it will retrieve a list of all of the pertinent data contained in the selected column.
For instance, in MySQL:
Code:
Name | Age
-------------
Mike | 12
Tim | 19
Sal | 11
SELECT GROUP_CONCAT(Age) AS AgeList
FROM aboveTable;
AgeList
--------
11,12,19
-Fine, back to Oracle ...
I found this excellent function on the web to and merely renamed the function 'GROUP_CONCAT'. However, with the same table and query- I get the following results:
Code:
AgeList
--------
12,19,11
Here is the link for the function I created:
And here is the code:
Code:
CREATE OR REPLACE TYPE t_string_agg AS OBJECT
(
g_string VARCHAR2(32767),
STATIC FUNCTION ODCIAggregateInitialize(sctx IN OUT t_string_agg)
RETURN NUMBER,
MEMBER FUNCTION ODCIAggregateIterate(self IN OUT t_string_agg,
value IN VARCHAR2 )
RETURN NUMBER,
MEMBER FUNCTION ODCIAggregateTerminate(self IN t_string_agg,
returnValue OUT VARCHAR2,
flags IN NUMBER)
RETURN NUMBER,
MEMBER FUNCTION ODCIAggregateMerge(self IN OUT t_string_agg,
ctx2 IN t_string_agg)
RETURN NUMBER
);
/
SHOW ERRORS
CREATE OR REPLACE TYPE BODY t_string_agg IS
STATIC FUNCTION ODCIAggregateInitialize(sctx IN OUT t_string_agg)
RETURN NUMBER IS
BEGIN
sctx := t_string_agg(NULL);
RETURN ODCIConst.Success;
END;
MEMBER FUNCTION ODCIAggregateIterate(self IN OUT t_string_agg,
value IN VARCHAR2 )
RETURN NUMBER IS
BEGIN
SELF.g_string := self.g_string || ',' || value;
RETURN ODCIConst.Success;
END;
MEMBER FUNCTION ODCIAggregateTerminate(self IN t_string_agg,
returnValue OUT VARCHAR2,
flags IN NUMBER)
RETURN NUMBER IS
BEGIN
returnValue := RTRIM(LTRIM(SELF.g_string, ','), ',');
RETURN ODCIConst.Success;
END;
MEMBER FUNCTION ODCIAggregateMerge(self IN OUT t_string_agg,
ctx2 IN t_string_agg)
RETURN NUMBER IS
BEGIN
SELF.g_string := SELF.g_string || ',' || ctx2.g_string;
RETURN ODCIConst.Success;
END;
END;
/
SHOW ERRORS
CREATE OR REPLACE FUNCTION string_agg (p_input VARCHAR2)
RETURN VARCHAR2
PARALLEL_ENABLE AGGREGATE USING t_string_agg;
/
SHOW ERRORS
Any thoughts or suggestions on how I can best achieve an ordered list based on the code above? Any input is greatly appreciated.
Thanks in advance,
Mike
[blue]Go to work to learn. Don't go to work to earn.[/blue]