Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have only been able to test this on Oracle 11g R2; however, I believe that everything is also available for Oracle 10g.</p> <p>There are two functions included here both of which use collections:</p> <ul> <li>The first function is simpler;</li> <li>The second function makes use of the <code>DBMS_LOB</code> package and is more verbose but, on my tests, has seemed to be much much more efficient (although I would recommend profiling it yourself to test it).</li> </ul> <p><a href="http://sqlfiddle.com/#!4/874a9/2" rel="nofollow">SQL Fiddle</a></p> <p><strong>Oracle 11g R2 Schema Setup</strong>:</p> <p>For this method, you will need to define a Collection to aggregate the strings into:</p> <pre><code>CREATE OR REPLACE TYPE VARCHAR2s_Table AS TABLE OF VARCHAR2(4000); / </code></pre> <p>This function takes a string collection (and an optional delimiter) and returns a <code>CLOB</code> containing the concatenated strings - if you have a smaller dataset (as per your example) then this is probably overkill and you can replace <code>CLOB</code> with <code>VARCHAR2</code>.</p> <pre><code>CREATE OR REPLACE FUNCTION concatStrings( Strs VARCHAR2s_Table, delim VARCHAR2 DEFAULT ',' ) RETURN CLOB AS out_string CLOB; BEGIN FOR i IN 1 .. Strs.COUNT LOOP out_string := out_string || CASE WHEN i = 1 THEN '' ELSE delim END || Strs(i); END LOOP; RETURN out_string; END; / </code></pre> <p>However, if you are going to have a long string returned as a <code>CLOB</code> then it may be more efficient to use some of the functions of the <code>DBMS_LOB</code> package:</p> <pre><code>CREATE OR REPLACE FUNCTION concatStrings2( Strs VARCHAR2s_Table, delim VARCHAR2 DEFAULT ',' ) RETURN CLOB AS out_string CLOB; dl CONSTANT NUMBER(10) := LENGTH( delim ); BEGIN DBMS_LOB.CREATETEMPORARY( out_string, TRUE ); IF strs IS NOT NULL AND strs IS NOT EMPTY THEN IF dl &gt; 0 THEN DBMS_LOB.WRITEAPPEND( out_string, LENGTH( strs(1) ), strs(1) ); FOR i IN 2 .. strs.COUNT LOOP DBMS_LOB.WRITEAPPEND( out_string, dl, delim ); DBMS_LOB.WRITEAPPEND( out_string, LENGTH( strs(i) ), strs(i) ); END LOOP; ELSE FOR i IN 1 .. strs.COUNT LOOP DBMS_LOB.WRITEAPPEND( out_string, LENGTH( strs(i) ), strs(i) ); END LOOP; END IF; END IF; RETURN out_string; END concatStrings2; / </code></pre> <p>Your test data:</p> <pre><code>CREATE TABLE tbl1 ( POD, name ) AS SELECT 'North', 'Rony' FROM DUAL UNION ALL SELECT 'North', 'James' FROM DUAL UNION ALL SELECT 'North', 'Aby' FROM DUAL UNION ALL SELECT 'South', 'Sam' FROM DUAL UNION ALL SELECT 'South', 'Willy' FROM DUAL UNION ALL SELECT 'West', 'Mike' FROM DUAL / </code></pre> <p><strong>Query 1</strong>:</p> <pre><code>SELECT POD, concatStrings( CAST( COLLECT( name ORDER BY name ASC ) AS VARCHAR2s_Table )) AS name FROM tbl1 GROUP BY POD </code></pre> <p><strong><a href="http://sqlfiddle.com/#!4/874a9/2/0" rel="nofollow">Results</a></strong>:</p> <pre><code>| POD | NAME | |-------|----------------| | North | Aby,James,Rony | | South | Sam,Willy | | West | Mike | </code></pre> <p><strong>Query 2</strong>:</p> <pre><code>SELECT POD, concatStrings2( CAST( COLLECT( name ORDER BY name ASC ) AS VARCHAR2s_Table )) AS name FROM tbl1 GROUP BY POD </code></pre> <p><strong><a href="http://sqlfiddle.com/#!4/874a9/2/1" rel="nofollow">Results</a></strong>:</p> <pre><code>| POD | NAME | |-------|----------------| | North | Aby,James,Rony | | South | Sam,Willy | | West | Mike | </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload