Oracle multi-row Merge row method

Source: Internet
Author: User

If there is a table below, where the number of rows for each I value is variable

SQL code
  1. Sql> SELECT * from t;
  2. I A D
  3. ---------- ---------- -------------------
  4. 1 b 2008-03-27 10:55:42
  5. 1 a 2008-03-27 10:55:46
  6. 1 D 2008-03-27 10:55:30
  7. 2 Z 2008-03-27 10:55:55
  8. 2 T 2008-03-27 10:55:59
  9. ---to get the following result, note that the string needs to be sorted by the time of column D:
  10. 1 d,b,a
  11. 2 z,t



This is a more typical row and column conversion, there are several ways to implement

1. Custom Function Implementations

SQL code
  1. Create or replace function My_concat (n number)
  2. return VARCHAR2
  3. Is
  4. Type typ_cursor is ref cursor;
  5. V_cursor Typ_cursor;
  6. V_temp VARCHAR2 (10);
  7. V_result varchar2 (4000): = ";
  8. V_sql VARCHAR2 (200);
  9. Begin
  10. V_sql: = ' Select a from t where i= ' | | | n | |  ' ORDER by d ';
  11. Open v_cursor for v_sql;
  12. Loop
  13. fetch v_cursor into v_temp;
  14. Exit when V_cursor%notfound;
  15. V_result: = V_result | |  ', ' | | | v_temp;
  16. end Loop;
  17. return substr (v_result,2);
  18. End
  19. sql> Select I,my_concat (i) from the T Group by I;
  20. I my_concat (i)
  21. ---------- --------------------
  22. 1 d,b,a
  23. 2 z,t



Although this approach can achieve the requirements, but if the data volume of table t is large, I the value of a lot of cases, because for each I value to execute a SELECT, scan and sort the number of times and the value of I is proportional to the performance will be very poor.

2. Using Sys_connect_by_path

SQL code
  1. Select I,ltrim (max (Sys_connect_by_path (A,', ')),', ') a
  2. From
  3. (
  4. Select I,a,d,min (d) over (partition by i) d_min,
  5. (Row_number () over (order by I,d)) + (Dense_rank () over (order by i)) numid
  6. From T
  7. )
  8. Start with d=d_min connect by numid-1=prior numid
  9. Group by I;


From the execution plan, this method only needs to scan two tables, more efficient than the method of custom functions, especially when the data volume in the table is large:

3. Using Wm_sys.wm_concat
This function can also implement a similar row-and-column conversion requirement, but there seems to be no way to sort directly according to the other columns, so you need to first order through a subquery or temporary table:

SQL code
  1. sql> Select I,wmsys.wm_concat (a) from T Group by I;
  2. I Wmsys. Wm_concat (A)
  3. ---------- --------------------
  4. 1 b,a,d
  5. 2 z,t
  6. sql> Select I,wmsys.wm_concat (a)
  7. 2 from
  8. 3 (select * from t order by I,d)
  9. 4 GROUP by I;
  10. I Wmsys. Wm_concat (A)
  11. ---------- --------------------
  12. 1 d,b,a
  13. 2 z,t


On the execution plan, only one table scan is required, but the function is encrypted, and the execution plan does not show the action inside the function.

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.