Group string addition in Database

Source: Internet
Author: User

-- This test script can be run directly.

-- Now we want to group and add data in the database by fixed fields. Five methods are summarized here.

-- Create a test table and add Test Data

 
 
  1. create table test(id varchar2(10),mc varchar2(50));   
  2. insert into test values('1','11111');   
  3. insert into test values('1','22222');   
  4. insert into test values('2','11111');   
  5. insert into test values('2','22222');   
  6. insert into test values('3','11111');   
  7. insert into test values('3','22222');   
  8. insert into test values('3','33333');   
  9. commit; 

-- Method 1:

 
 
  1. set serveroutput on size 1000000   
  2. declare   
  3. union_mc varchar2(200);   
  4. begin   
  5. for cur_a in(select distinct id from test) loop   
  6. for cur_b in(select mc from test where id=cur_a.id) loop   
  7. union_mc:=union_mc||cur_b.mc;   
  8. end loop;   
  9. dbms_output.put_line(cur_a.id||chr(9)||union_mc);   
  10. union_mc := '';   
  11. end loop;   
  12. end;   
  13. /  

-- Method 2:

 
 
  1. CREATE OR REPLACE function link(v_id varchar2)   
  2. return varchar2   
  3. is   
  4. union_mc varchar2(200);   
  5. begin   
  6. for cur in (select mc from test where id=v_id) loop   
  7. union_mc := union_mc||cur.mc;   
  8. end loop;   
  9. union_mc := rtrim(union_mc,1);   
  10. return union_mc;   
  11. end;   
  12. /   
  13.  
  14. select id,link(id) from test group by id; 

-- Method 3:

/* Starting from Oracle 9i, developers can create user-defined aggregate functions. Apart from PL/SQL, they can also use any languages supported by Oralce, such as C ++ or Java) to create Aggregate functions. The TYPE header definition must containOdciaggresponinitialize,ODCIAggregateIterate,ODCIAggregateMergeAndOdciaggresponterminateThese four interface functions. */

/* InitializeThe function runs each field to be processed in the Data Group once. Naturally, I need to prepare a new list for each value, so I need to initialize the persistent variable.ListHere, the initialization value isNull. */

/* IterateThe function processes the returned rows, so it is actually used to create a list of returned values. Test firstListWhether it is null. If it is null, SetListDirectly set to the introducedValueValue; ifListIf the variable is not empty, add a comma to the list and insert it again.ValueValue,ListThe maximum number of characters allowed is 32767. */

/* TerminateThe function is executed after the field data of interest in each row in the Data Group is processed. In this function, I only need to simply return the list variable. */

/* MergeFunction, used to return the success mark. */

/* Create your own Aggregate functions to expand Oracle statistics and text processing capabilities. */

 
 
  1. create or replace type t_cat as object   
  2. (   
  3. union_mc VARCHAR2(200),   
  4. static function ODCIAggregateInitialize(sctx IN OUT t_cat) return number,   
  5. member function ODCIAggregateIterate(self IN OUT t_cat,value IN varchar2) return number,   
  6. member function ODCIAggregateTerminate(self IN t_cat,returnValue OUT varchar2, flags IN number) return number,   
  7. member function ODCIAggregateMerge(self IN OUT t_cat,ctx2 IN t_cat) return number   
  8. );   
  9.  
  10. create or replace type body t_cat is   
  11. static function ODCIAggregateInitialize(sctx IN OUT t_cat )   
  12. return number is   
  13. begin   
  14. sctx := t_cat('');   
  15. return ODCIConst.Success;   
  16. end;   
  17. member function ODCIAggregateIterate(self IN OUT t_cat, value IN varchar2)   
  18. return number is   
  19. begin   
  20. self.union_mc := self.union_mc || value;   
  21. return ODCIConst.Success;   
  22. end;   
  23. member function ODCIAggregateTerminate(self IN t_cat, returnValue OUT varchar2, flags IN number) return number is   
  24. begin   
  25. returnValue := self.union_mc;   
  26. return ODCIConst.Success;   
  27. end;   
  28. member function ODCIAggregateMerge(self IN OUT t_cat , ctx2 IN t_cat ) return number is   
  29. begin   
  30. return ODCIConst.Success;   
  31. end;   
  32. end;   

/* If your Oracle server is not configured to support parallel processing, remove the PARALLEL_ENABLE parameter */

Create or replace function catstr (v_mc varchar2) return varchar2 PARALLEL_ENABLE aggregate using t_cat;
/

Select id, catstr (mc) from test group by id;

-- Method 4:

-- Oracle9i or later

 
 
  1. select id,ltrim(max(sys_connect_by_path(mc,';')),';') from(   
  2. select id,mc,row_number() over(partition by id order by id) id1,   
  3. row_number() over(order by id) + dense_rank() over(order by id) id2   
  4. from test   
  5. )   
  6. start with id1=1 connect by prior id2 = id2 -1   
  7. group by id order by id;  

Another method of Method 4

It is estimated that there are still a lot of similar writing methods. This is different from the previous one in the use of connect without the start withfilter function, and is implemented using level and first_value.

 
 
  1. SELECT distinct id,ltrim(first_value(mc_add) over (partition by id order BY l DESC),';')   
  2. from (   
  3. SELECT id,LEVEL l,sys_connect_by_path(mc,';') mc_add from   
  4. (   
  5. select id||rownum rn,id||rownum-1 rn_small,id,mc from test   
  6. )   
  7. CONNECT BY PRIOR rn = rn_small   
  8. )   

Method 5:

 
 
  1. select id,wm_concat(mc) from test group by id 

Related Article

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.