Two oracle transpose Methods

Source: Internet
Author: User

Two oracle transpose Methods: Method 1: SYS_CONNECT_BY_PATH, ROW_NUMBER () OVER (partition .. order ..), start with, connect by prior Combination Method 2: wmsys. wm_concat example: In table 1, one col1 corresponds to multiple col2 fields. In the following example, we need to convert col2 to a format such as 2 3 4 where the col2 field value is 2 3 4 and it needs to be changed to 2-3-4, in addition, the value of col2 cannot be enumerated by thousands or tens of thousands. In this way, some other transpose methods through decode cannot implement the select tt method 1 SQL code. col1, '-' | ':' | TO_CHAR (SUBSTR (MAX (SYS_CONNECT_BY_PATH (TT. col2, '-'), 2) M -- to intercept the "-" FROM (select t. col1, T. col2, T. col1 + ROW_NUMBER () OVER (partition by t. col1 order by t. col2) RN, ROW_NUMBER () OVER (partition by t. col1 order by t. col2) RM -- the preceding two rows use two ROW_NUMBER () times because col1 is the cumulative value, so a T. col1 + ROW_NUMBER () is used to distinguish different groups. ROW_NUMBER () is used to set the start value of recursion, however, this value is "1" for different groups, so we need to use two FROM table1 t where/* T. col1 = TO_NUMBER ('000000') */) TT -- note the start with rm = 1 connect by prior rn + 1 = rn group by tt. col1; Use method 2 SQL code select substr (tt. co, 1, length (tt. co)-1), -- end with "-" tt. col1 from (select t. col1, replace (wmsys. wm_concat (t. col2 | '-'), ',', null) co -- remove "," from table1 t -- where t. col1 = TO_NUMBER ('123') group by t. col1) tt; the first one was previously written, and the second one was actually simpler.

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.